24. 两两交换链表中的节点
思路讲解:代码随想录:两两交换链表中的节点
难度:中等
(1)、 采用虚拟头结点dummy比较方便,不用单独处理头结点
(2)、 链表:dummy -> 1 -> 2 -> 3 -> 4
- dummy -> 2
- 1 -> 3
- 2 -> 1
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode first = new ListNode();
ListNode second = new ListNode();
ListNode cur = dummy;
while(cur.next!=null && cur.next.next!=null){
first = cur.next;
second = cur.next.next;
cur.next = second;
first.next = second.next;
second.next = first;
cur = cur.next.next;
}
return dummy.next;
}
}
19.删除链表的倒数第N个节点
思路讲解:代码随想录:删除链表的倒数第N个节点
难度:中等
- 定义fast指针和slow指针,初始值为虚拟头结点
- fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作)
- - fast和slow同时移动,直到fast指向末尾
- 删除slow指向的下一个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
for(int i=0; i<=n; i++){
fast = fast.next;
}
// fast先走n+1步,让slow指针指向要删除结点的上一个结点
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
if (slow.next != null){
slow.next = slow.next.next;
}
return dummy.next;
}
}
面试题 02.07. 链表相交
思路讲解:代码随想录: 链表相交
难度:简单
(1)、相交是指两指针相等
(2)、思路1:
- 两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置
- 比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int temp = lenA;
lenA = lenB;
lenB = temp;
//2. swap (curA, curB);
ListNode tempNode = curA;
curA = curB;
curB = tempNode;
}
// 让curA和curB在同一起点上(末尾位置对齐)
while (lenA > lenB) {
curA = curA.next;
lenA--;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
(3)、合并链表实现同步移动
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// p1 指向 A 链表头结点,p2 指向 B 链表头结点
ListNode p1 = headA, p2 = headB;
while (p1 != p2) {
// p1 走一步,如果走到 A 链表末尾,转到 B 链表
if (p1 == null) p1 = headB;
else p1 = p1.next;
// p2 走一步,如果走到 B 链表末尾,转到 A 链表
if (p2 == null) p2 = headA;
else p2 = p2.next;
}
return p1;
}
}
142.环形链表II
思路讲解:代码随想录:环形链表II
难度:中等
(1)、判断链表是否有环
使用快慢指针法:分别定义 fast 和 slow 指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow指针在途中相遇 ,说明这个链表有环。
(2)、找到环的入口
- 假设从头结点到环形入口节点 的节点数为x。 环形入口节点到 fast指针与slow指针相遇节点 节点数为y。 从相遇节点 再到环形入口节点节点数为 z
- 相遇时: slow指针走过的节点数为: x + y, fast指针走过的节点数:x + y + n (y +
z),n为fast指针在环内走了n圈才遇到slow指针, (y+z)为 一圈内节点的个数A。 - 因为fast指针是一步走两个节点,slow指针一步走一个节点, 所以 fast指针走过的节点数 = slow指针走过的节点数 * 2:
(x + y) * 2 = x + y + n (y + z)
两边消掉一个(x+y): x + y = n (y + z) - 因为要找环形的入口,那么要求的是x,因为x表示头结点到环形入口节点的距离。
要求x ,将x单独放在左面:x = n (y + z) - y , - 再从n(y+z)中提出一个 (y+z)来,整理公式之后为如下公式:x = (n - 1) (y + z) + z
注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针。 - 这个公式说明什么呢?
先拿n为1的情况来举例,意味着fast指针在环形里转了一圈之后,就遇到了 slow指针了。
当 n为1的时候,公式就化解为 x = z,
这就意味着,从头结点出发一个指针,从相遇节点也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是环形入口的节点。
也就是在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。
让index1和index2同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点。 - 那么 n如果大于1是什么情况呢,就是fast指针在环形转n圈之后才遇到 slow指针。
其实这种情况和n为1的时候 效果是一样的,一样可以通过这个方法找到 环形的入口节点,只不过,index1 指针在环里 多转了(n-1)圈,然后再遇到index2,相遇点依然是环形的入口节点。
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast !=null && fast.next !=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast) {
ListNode index1 = fast;
ListNode index2 = head;
while(index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}