Skip to content

Commit 6b59314

Browse files
authored
Merge pull request #22 from daluozha/main
leetcode 160、328,剑指offer22 补充js代码
2 parents b47322c + cedb32f commit 6b59314

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

animation-simulation/链表篇/leetcode328奇偶链表.md

+16
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,19 @@ public:
8989
};
9090
```
9191

92+
JS Code:
93+
```javascript
94+
var oddEvenList = function(head) {
95+
if(!head || !head.next) return head;
96+
let odd = head, even = head.next, evenHead = even;
97+
while(odd.next && even.next){
98+
odd.next = even.next;
99+
odd = odd.next;
100+
even.next = odd.next;
101+
even = even.next;
102+
}
103+
odd.next = evenHead;
104+
return head;
105+
};
106+
```
107+

animation-simulation/链表篇/leetcode相交链表.md

+30
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ public:
7979
};
8080
```
8181
82+
JS Code:
83+
```javascript
84+
var getIntersectionNode = function(headA, headB) {
85+
let tempa = headA, tempb = headB
86+
const map = new Map()
87+
while(tempa){
88+
map.set(tempa, 1)
89+
tempa = tempa.next
90+
}
91+
while(tempb){
92+
if(map.get(tempb))
93+
return tempb
94+
tempb = tempb.next
95+
}
96+
return tempb
97+
};
98+
```
99+
82100
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
83101

84102
下面我们直接看动图吧,特别直观,一下就可以搞懂。
@@ -128,6 +146,18 @@ public:
128146
};
129147
```
130148
149+
JS Code:
150+
```javascript
151+
var getIntersectionNode = function(headA, headB) {
152+
let tempa = headA, tempb = headB
153+
while(tempa !== tempb){
154+
tempa = tempa ? tempa.next : headB
155+
tempb = tempb ? tempb.next : headA
156+
}
157+
return tempa
158+
};
159+
```
160+
131161
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
132162

133163

animation-simulation/链表篇/剑指offer2倒数第k个节点.md renamed to animation-simulation/链表篇/剑指offer22倒数第k个节点.md

+15
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ public:
9393
};
9494
```
9595
96+
JS Code:
97+
```javascript
98+
var getKthFromEnd = function(head, k) {
99+
if(!head) return head;
100+
let pro = head, after = head;
101+
for(let i = 0; i < k - 1; i++){
102+
pro = pro.next;
103+
}
104+
while(pro.next){
105+
pro = pro.next;
106+
after = after.next;
107+
}
108+
return after;
109+
};
110+
```

0 commit comments

Comments
 (0)