24.两两交换列表中的节点
解题思路:定义一个虚拟节点dummy,两个相邻的节点交换需要三个步骤:dummy➡️2,2➡️1,1➡️3,dummy后移两位继续循环,当dummy.next || dummy.next.next 为null,终止循环。
Java代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
ListNode temp1 = null;
ListNode temp3 = null;
while(cur.next != null && cur.next.next != null) {
temp1 = cur.next; //保存第1个节点
temp3 = cur.next.next.next; //保存第3个节点
cur.next = cur.next.next; //dummy节点➡️第2个节点
cur.next.next = temp1; //第2个节点➡️第1个节点
temp1.next = temp3; //第1个节点➡️第3个节点
//cur后移两位
cur = cur.next.next;
}
return dummy.next;
}
}
19.删除链表中倒数第N个节点
解题思路:双指针法,fast节点先走N步,然后slow和fast同时后移,while终止条件为:fast.next为null时终止
Java代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
//快指针走N步
for (int i= 0; i < n; i++) {
fast = fast.next;
}
//快、慢指针同时后移
while(fast.next != null) {
fast = fast.next;
slow = slow.next;
}
//此时slow指向的是删除节点的前驱节点
//slow指向删除节点的下一个节点
slow.next = slow.next.next;
return dummy.next;
}
}
02.07链表相交
解题思路:
-
设置两个当前节点,分别指向headA、headB
-
计算出curA、curB的长度
-
再次把当前节点分别指向头节点headA、headB
-
将比较长的链表后移,然后两个链表剩余的长度一致(此时较长的cur已经后移)
-
while循环curA、curB,如果相同,直接返回curA;curA、curB后移一位,直到条件终止
Java代码:
/**
* 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;
//计算A和B的长度
while(curA != null) {
curA = curA.next;
LenA++;
}
while(curB != null) {
curB = curB.next;
LenB++;
}
//curA curB重新指向头节点
curA = headA;
curB = headB;
//将较长的链表指向向后移,使两个链表的剩余长度一致
if(LenA > LenB){
for (int i =0; i < LenA - LenB; i++) {
curA = curA.next;
}
} else {
for (int i =0; i < LenB - LenA; i++) {
curB = curB.next;
}
}
//同时循环curA curB,找到相交点
while (curA != null && curB != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
142.环形入口节点
解题思路:
-
设置fast、slow两个节点,fast走两步,slow走一步,如果相同,则为相遇点,说明有环
-
有环,则设置index1相遇点、index2头节点,同时后移,如果相同,则为入环节点处
Java代码:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) { //有环 相遇点
//设置头节点、fast节点,循环后移,如果相同,那么该节点就是入环节点处
ListNode index1 = fast;
ListNode index2 = head;
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}