Merge pull request #22 from daluozha/main

leetcode 160、328,剑指offer22 补充js代码
This commit is contained in:
算法基地 2021-05-06 13:39:35 +08:00 committed by GitHub
commit 6b59314ce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -89,3 +89,19 @@ public:
}; };
``` ```
JS Code:
```javascript
var oddEvenList = function(head) {
if(!head || !head.next) return head;
let odd = head, even = head.next, evenHead = even;
while(odd.next && even.next){
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
};
```

View File

@ -79,6 +79,24 @@ public:
}; };
``` ```
JS Code:
```javascript
var getIntersectionNode = function(headA, headB) {
let tempa = headA, tempb = headB
const map = new Map()
while(tempa){
map.set(tempa, 1)
tempa = tempa.next
}
while(tempb){
if(map.get(tempb))
return tempb
tempb = tempb.next
}
return tempb
};
```
下面这个方法比较巧妙不是特别容易想到大家可以自己实现一下这个方法也是利用我们的双指针思想 下面这个方法比较巧妙不是特别容易想到大家可以自己实现一下这个方法也是利用我们的双指针思想
下面我们直接看动图吧特别直观一下就可以搞懂 下面我们直接看动图吧特别直观一下就可以搞懂
@ -128,6 +146,18 @@ public:
}; };
``` ```
JS Code:
```javascript
var getIntersectionNode = function(headA, headB) {
let tempa = headA, tempb = headB
while(tempa !== tempb){
tempa = tempa ? tempa.next : headB
tempb = tempb ? tempb.next : headA
}
return tempa
};
```
好啦链表的题目就结束啦希望大家能有所收获下周就要更新新的题型啦继续坚持肯定会有收获的 好啦链表的题目就结束啦希望大家能有所收获下周就要更新新的题型啦继续坚持肯定会有收获的

View File

@ -93,3 +93,18 @@ public:
}; };
``` ```
JS Code:
```javascript
var getKthFromEnd = function(head, k) {
if(!head) return head;
let pro = head, after = head;
for(let i = 0; i < k - 1; i++){
pro = pro.next;
}
while(pro.next){
pro = pro.next;
after = after.next;
}
return after;
};
```