代码随想录day4
- [24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/)
- [19. 删除链表的倒数第 N 个结点](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/)
- [面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/)
- [142. 环形链表 II](https://leetcode.cn/problems/linked-list-cycle-ii/description/)
24. 两两交换链表中的节点
思路
定义一个虚拟头节点,使cur指针等于虚拟头结点,然后使用cur指针交换它后的两个节点,再移动cur,直到链表循环结束。
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点
dumyhead.next = head;
ListNode cur = dumyhead;
ListNode temp1;
ListNode temp2;
while(cur.next != null && cur.next.next != null){
temp1 = cur.next; //保存cur节点后的第一个节点
temp2 = cur.next.next.next; //保存cur节点后的第三个节点
cur.next = cur.next.next; //cur后的两个元素交换位置,所以cur的next指向原本cur的next的next
cur.next.next = temp1;
temp1.next = temp2;
cur = temp1;
}
return dumyhead.next;
}
}
总结
循环条件的顺序必须是cur.next != null && cur.next.next != null,如果cur.next.next != null在前的话,会导致cur已经是最后一位元素(cur.next = null)时,cur.next.next报空指针异常。
还需要注意需要两个临时节点防止交换节点时断链。
19. 删除链表的倒数第 N 个结点
如果要删除倒数第n个节点,让fast移动n+1步,然后让fast和slow同时移动,直到fast指向链表末尾,此时slow指向的就是被删除元素的前驱节点,删掉slow指向节点的next节点就可以了。
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
for(int i = 0; i <= n; i++){ //这里要循环n+1此,这样slowIndex才能指向被删除节点的前驱结点
fastIndex = fastIndex.next;
}
while(fastIndex != null){
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
slowIndex.next = slowIndex.next.next;
return dummyNode.next;
}
}
面试题 02.07. 链表相交
这道题是求两个链表相交的节点,也就是说两条链表从这个节点之后的所有节点都相同,那么这个节点之后的长度就是一样的。我们可以先遍历两条链表,求出长度差值之后,先将长链表中的遍历指针移动到与短链表同一位置,然后同时移动两条链表上的指针,比较节点是否相同。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0;
int lenB = 0;
int gap = 0;
if(curA == null || curB == null){
return null;
}
while(curA.next != null){
lenA++;
curA = curA.next;
}
while(curB.next != null){
lenB++;
curB = curB.next;
}
if(lenA > lenB){
curA = headA; //使curA指向更长的链表头节点
curB = headB;
gap = lenA - lenB; //求两条链表长度差值
}else{
curA = headB;
curB = headA;
gap = lenB - lenA;
}
for(int i = 0; i < gap; i++){ //先移动长链表的指针
curA = curA.next;
}
while(curA != null){
if(curA == curB){
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
142. 环形链表 II
非常巧妙的一道题,还是双指针的妙用。设置fast和slow两个指针,fast每次向后移动两步,slow每次移动一步,这样在环形列表之前,fast和slow只会越来越远,而进入环形链表之后,fast每次比slow多移动一步,每次移动它们的间距都会减一,所以fast一定能追上slow。
假设链表长度为a+b, 链表头部到链表入口 有 a个节点(不计链表入口节点), 链表环有 b 个节点,那么第一次相遇时,fast移动的步数等于2slow,fast比slow多走n圈环路,所以f = 2s = s + n * b,那么s = n * b, f = 2n * b;
也就是说,第一次相遇时,fast和slow分别走了2n倍环路的长度和n倍环路的长度。
此时,假设一个节点从头结点出发,它能经过入口节点的步数只能是 k = a + n * b(第一次到达入口节点是a,之后每多走b步就又经过一次入口节点)
而此时slow走了n * b步,那么,slow还需要再走a步(也就是总共走a + n * b步)才能再次到达入口节点。
那么如何让slow走a步呢?a正好是链表头部到链表入口的节点个数,所以我们让fast指针再重新指向head节点,然后和slow一起向后逐个节点遍历,当fast移动a步刚好到入口节点,slow移动a步也刚好到入口节点,所以当fast和slow指针指向同一节点时,这个节点就是入口节点。
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(true){
if(fast == null || fast.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if(fast == slow) break;
}
fast = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
}