# 代码随想录Day4-Leetcode24-两两交换链表中的节点， 19.删除链表的倒数第N个节点， 142.环形链表II()-其他

## 代码随想录Day4-Leetcode24-两两交换链表中的节点， 19.删除链表的倒数第N个节点， 142.环形链表II()

### 24.两两交换链表中的节点

``````
/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
}
while(true){
let tmp = p2.next
p2.next = p1
p1.next = tmp
pre.next = p2
tmp = p1
p1 = p2
p2 = tmp
for(let i = 0 ;i<2;i++){
if(p2.next!=null){
p2 =p2.next
p1 = p1.next
pre = pre.next
}else{
}
}

}
};
``````

### 19.删除链表的倒数第N个节点

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {

for(let i =0; i<n;i++){
fast = fast.next
}
while(fast.next!=null){
fast = fast.next
slow = slow.next
}
slow.next = slow.next.next
};
``````

### 面试题 02.07. 链表相交

``````/**
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/

/**
* @return {ListNode}
*/
//同样可以快慢指针, 但需要数学推导
//如果可用map或set
let set = new Set()

while(pa!=null){
pa = pa.next
}
while(pb!=null){
if(set.has(pb)){
return pb
}
pb = pb.next
}
return null

};
``````

### 142.环形链表II

``````/**
* @return {ListNode}
*/
//思路:快慢指针,最后会相遇,但我忘记算法了
//map?
return null
}
while(true){
for(let i = 0; i<2; i++){
if(fast.next!=null){
fast = fast.next
}else{
return null
}
}
slow = slow.next
if(slow === fast){
//相遇//很多时候不能看语句的数量来评价算法复杂度, 就这句能写出来,不简单
while(slow!==fast){
fast = fast.next
slow = slow.next
}
return slow
}
//a b
//s 2s = s+nb
//s= nb
//到交叉点的路程a+nb
}
};
``````
————————

### 24.两两交换链表中的节点

``````
/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
}
while(true){
let tmp = p2.next
p2.next = p1
p1.next = tmp
pre.next = p2
tmp = p1
p1 = p2
p2 = tmp
for(let i = 0 ;i<2;i++){
if(p2.next!=null){
p2 =p2.next
p1 = p1.next
pre = pre.next
}else{
}
}

}
};
``````

### 19.删除链表的倒数第N个节点

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {

for(let i =0; i<n;i++){
fast = fast.next
}
while(fast.next!=null){
fast = fast.next
slow = slow.next
}
slow.next = slow.next.next
};
``````

### 面试题 02.07. 链表相交

``````/**
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/

/**
* @return {ListNode}
*/
//同样可以快慢指针, 但需要数学推导
//如果可用map或set
let set = new Set()

while(pa!=null){
pa = pa.next
}
while(pb!=null){
if(set.has(pb)){
return pb
}
pb = pb.next
}
return null

};
``````

### 142.环形链表II

``````/**
* @return {ListNode}
*/
//思路:快慢指针,最后会相遇,但我忘记算法了
//map?
return null
}
while(true){
for(let i = 0; i<2; i++){
if(fast.next!=null){
fast = fast.next
}else{
return null
}
}
slow = slow.next
if(slow === fast){
//相遇//很多时候不能看语句的数量来评价算法复杂度, 就这句能写出来,不简单