链表高频

(一)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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值