20230815 | LinkedList Part2

Leetcode24 Swap Nodes in Pairs

题目链接

swap nodes

Method1

找到2个临时ListNode, 按顺序换

class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null) return null;
        ListNode dummy = new ListNode(-1), p = dummy;
        dummy.next = head;

        while (p.next != null && p.next.next != null){
            ListNode tmp = p.next, tmp2 = p.next.next.next;
            p.next = p.next.next;
            p.next.next = tmp;
            p.next.next.next = tmp2;
            p = p.next.next;
        }
        return dummy.next;
    }
}

Method2 递归

可以看成后序遍历, 需要返回newNode为后面所用

class Solution {
    public ListNode swapPairs(ListNode head) {
        // base case
        if (head == null || head.next == null) return head;

        ListNode next = head.next;
        ListNode newNode = swapPairs(next.next);
        next.next = head;
        head.next = newNode;

        return next;
    }
}

Leetcode19 Remove Nth Node From End of List

题目链接
双指针法, fast先移到第n位, 再fast/slow同时移

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) return null;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = dummy;
        for (int i = 0; i < n; i++){
            fast = fast.next;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        
        slow.next = slow.next.next;
        return dummy.next;
    }
}

Leetcode160 Intersection of Two Linked Lists

题目链接

Method1 HashSet

用HashSet记录判断是否为同一地址的ListNode

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        while (headA != null){
            set.add(headA);
            headA = headA.next;
        }
        while (headB != null){
            if (set.contains(headB)) return headB;
            headB = headB.next;
        }
        return null;
    }
}

Method2 双指针

求出两个链表的长度,并求出两个链表长度的差值,然后让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 tmpLen = lenA;
            lenA = lenB;
            lenB = tmpLen;
            //2. swap (curA, curB);
            ListNode tmpNode = curA;
            curA = curB;
            curB = tmpNode;
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while (gap-- > 0) {
            curA = curA.next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }

}

Leetcode142 Linked List Cycle II

Method1 HashSet

time complexity: O(n)
space complexity: O(n) -> need to store all nodes

public class Solution {
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode result = new ListNode(-1);
        while (head != null){
            if (set.contains(head)){
                result = head;
                return result;
            }
            set.add(head);
            head = head.next;
            
        }
        return null;
    }
}

Method2 龟兔赛跑

龟兔赛跑

数学运算

  1. 先判断有无环
  2. 再判断环入口

time complexity: O(n)
space complexity: O(1)

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;

            if (slow == fast){//有环
                ListNode tmp1 = head, tmp2 = fast;
                while (tmp1 != tmp2){
                    tmp1 = tmp1.next;
                    tmp2 = tmp2.next;
                    
                }
                return tmp2;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值