File tree Expand file tree Collapse file tree 4 files changed +102
-0
lines changed Expand file tree Collapse file tree 4 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 18
18
19
19
下面我们来看一下题目代码,也是很容易理解。
20
20
21
+ Java Code:
21
22
``` java
22
23
class MyStack {
23
24
// 初始化队列
@@ -55,3 +56,34 @@ class MyStack {
55
56
56
57
```
57
58
59
+ JS Code:
60
+ ``` javascript
61
+ var MyStack = function () {
62
+ this .queue = [];
63
+ };
64
+
65
+ MyStack .prototype .push = function (x ) {
66
+ this .queue .push (x);
67
+ if (this .queue .length > 1 ) {
68
+ let i = this .queue .length - 1 ;
69
+ while (i) {
70
+ this .queue .push (this .queue .shift ());
71
+ i-- ;
72
+ }
73
+ }
74
+ };
75
+
76
+ MyStack .prototype .pop = function () {
77
+ return this .queue .shift ();
78
+ };
79
+
80
+ MyStack .prototype .top = function () {
81
+ return this .empty () ? null : this .queue [0 ];
82
+
83
+ };
84
+
85
+ MyStack .prototype .empty = function () {
86
+ return ! this .queue .length ;
87
+ };
88
+ ```
89
+
Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ class CQueue {
58
58
59
59
大家可以点击该链接[ 剑指 Offer 09. 用两个栈实现队列] ( https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/ ) 去实现一下,下面我们看代码。
60
60
61
+ Java Code:
61
62
``` java
62
63
class CQueue {
63
64
// 初始化两个栈
@@ -89,3 +90,24 @@ class CQueue {
89
90
}
90
91
```
91
92
93
+ JS Code:
94
+ ``` javascript
95
+ var CQueue = function () {
96
+ this .stack1 = [];
97
+ this .stack2 = [];
98
+ };
99
+
100
+ CQueue .prototype .appendTail = function (value ) {
101
+ this .stack1 .push (value);
102
+ };
103
+
104
+ CQueue .prototype .deleteHead = function () {
105
+ if (! this .stack2 .length ) {
106
+ while (this .stack1 .length ) {
107
+ this .stack2 .push (this .stack1 .pop ());
108
+ }
109
+ }
110
+ return this .stack2 .pop () || - 1 ;
111
+ };
112
+ ```
113
+
Original file line number Diff line number Diff line change 38
38
39
39
** 题目代码**
40
40
41
+ Java Code:
41
42
``` java
42
43
public class Solution {
43
44
public boolean hasCycle (ListNode head ) {
@@ -56,3 +57,18 @@ public class Solution {
56
57
}
57
58
```
58
59
60
+ JS Code:
61
+ ``` javascript
62
+ var hasCycle = function (head ) {
63
+ let fast = head;
64
+ let slow = head;
65
+ while (fast && fast .next ) {
66
+ fast = fast .next .next ;
67
+ slow = slow .next ;
68
+ if (fast === slow) {
69
+ return true ;
70
+ }
71
+ }
72
+ return false ;
73
+ };
74
+ ```
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
33
33
34
34
我会对每个关键点进行注释,大家可以参考动图理解。
35
35
36
+ Java Code:
36
37
``` java
37
38
class Solution {
38
39
public ListNode reverseList (ListNode head ) {
@@ -62,9 +63,27 @@ class Solution {
62
63
63
64
}
64
65
```
66
+ JS Code:
67
+ ``` javascript
68
+ var reverseList = function (head ) {
69
+ if (! head || ! head .next ) {
70
+ return head;
71
+ }
72
+ let low = null ;
73
+ let pro = head;
74
+ while (pro) {
75
+ let temp = pro;
76
+ pro = pro .next ;
77
+ temp .next = low;
78
+ low = temp;
79
+ }
80
+ return low;
81
+ };
82
+ ```
65
83
66
84
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
67
85
86
+ Java Code:
68
87
``` java
69
88
class Solution {
70
89
public ListNode reverseList (ListNode head ) {
@@ -86,3 +105,16 @@ class Solution {
86
105
87
106
```
88
107
108
+ JS Code:
109
+ ``` javascript
110
+ var reverseList = function (head ) {
111
+ if (! head || ! head .next ) {
112
+ return head;
113
+ }
114
+ let pro = reverseList (head .next );
115
+ head .next .next = head;
116
+ head .next = null ;
117
+ return pro;
118
+ };
119
+ ```
120
+
You can’t perform that action at this time.
0 commit comments