(一)copy list with random pointer
https://leetcode.com/problems/copy-list-with-random-pointer/description/
题目:复制一个含有random指针的链表;
解答:分为三步:1.复制链表值:1 -> 2 ->3 -> null 变成 1 -> 1' -> 2 -> 2' -> 3 -> 3' -> null;
2.复制random指针;
3.拆分链表;
第一次犯错:忘记考虑random是null的情况;
代码:
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return null;
}
RandomListNode dummy = new RandomListNode(0);
dummy.next = head;
//copy next
while (head != null) {
RandomListNode temp = head.next;
head.next = new RandomListNode(head.label);
head.next.next = temp;
head = temp;
}
//copy random
head = dummy.next;
while (head != null) {
if (head.random == null) {
head.next.random = null;
} else {
head.next.random = head.random.next;
}
head = head.next.next;
}
//split
head = dummy.next;
dummy.next = head.next;
while (head != null && head.next != null) {
RandomListNode temp = head.next;
head.next = head.next.next;
head = temp;
}
return dummy.next;
}
}
(二)linked list cycle
https://leetcode.com/problems/linked-list-cycle/description/
题目:判断链表是否有环;
解答:使用快慢两个指针,快指针每次移两位,慢指针每次移一位。若两指针能相遇,说明有环。
代码:
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (slow != null && fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
}
(三)linked list cycle II
https://leetcode.com/problems/linked-list-cycle-ii/description/
题目:若链表有环,返回环的开始位置;否则,返回空
解答:快指针的位置为慢指针的两倍,当快慢指针相遇后,慢指针距离环开始位置的距离比链表头距离环开始位置是距离远一个节点;
(设环的开始位置为a, 环的周长为b,两指针所在位置分别为x和2x,所以当两指针相遇时,其距离差恰好为环的周长,即 2x - x = b。此时,慢指针距离环开始位置的距离为:b - (x - a ) = a, 链表头部距离环开始位置的距离为:a - 1)
代码:
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast != slow) {
if (fast == null || fast.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
slow = slow.next;
while (head != slow) {
head = head.next;
slow = slow.next;
}
return head;
}
}
(四)intersection of two linked list
https://leetcode.com/problems/intersection-of-two-linked-lists/description/
题目:求两个单链表的交点;若无交点,返回空;
解答:将链表A的尾部与链表B的首部相连,判断新链表是否有环,若有,则返回环的起始位置(与linked list cycle II类似);
第一次犯错:忘记还原原链表结构;
代码:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode tailA = headA;
while (tailA.next != null) {
tailA = tailA.next;
}
tailA.next = headB;
ListNode res = detectCycle(headA);
tailA.next = null;
return res;
}
private ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head.next;
while (fast != slow) {
if (fast == null || fast.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
slow = slow.next;
while (head != slow) {
head = head.next;
slow = slow.next;
}
return head;
}
}

本文详细解析了四个经典的链表算法问题:复制含随机指针的链表、判断链表是否存在环、寻找链表环的入口及求解两个链表的交点。通过这些实例,不仅介绍了基本思路还提供了完整的Java代码实现。
881

被折叠的 条评论
为什么被折叠?



