Day4 - Leetcode 24Swap nodes in pairs | Leetcode 19删除链表倒数第N个节点 | 链表相交 | Leetcode 142环形链表

leetcode 24

题目链接
交换两两节点

思路:
  • 设置虚拟头结点dummy
  • 设置一个指针cur始终指向要交换的两个节点的前一个节点
  • 注意遍历终止——奇数个节点和偶数个节点情况
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        ListNode p, q;
        while (cur.next != null) {
            p = cur.next;
            q = cur.next.next;

            if (q == null)  //落单的元素,不用交换
                break;
            cur.next = q;
            p.next = q.next;
            q.next = p;
            cur = p;       //修改cur指向下一个要交换的
        }
        return dummy.next;
    }
}

leetcode 19

题目链接
**重点:**找到倒数第N个节点

思路:
  • 快慢指针
  • 慢指针slow最终指向要删除的前一个结点
  • 快指针fast先提前走N步,让快慢指针之间差N个节点
  • 然后同时移动,直到fast走到头
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        ListNode target = cur;
        
        for (int i = 0; i <= n; ++i)
            target = target.next;
        
        while (target != null) {
            cur = cur.next;
            target = target.next;
        }

        cur.next = cur.next.next;
        return dummy.next;
    }
}

面试题02.07

题目链接
找到两个链表相交(即相同)的元素节点

思路:
  • 双指针
  • 注意双指针不是同时向后移动的
  • 因为是后面相交,所以先找到与短的那个链表同的长度
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p = headA;
        ListNode q = headB;
        int aLen = 0, bLen = 0;

        while (p != null) {     //get len
            ++aLen;
            p = p.next;
        }
        while (q != null) {
            ++bLen;
            q = q.next;
        }
        p = headA;
        q = headB;

        if (aLen > bLen) {
            for (int i = 0; i < aLen - bLen; ++i)
                p = p.next;
            while (p != null) {
                if (p == q)
                    return p;
                p = p.next;
                q = q.next;
            }
            return null;
        }
        else {
            for (int i = 0; i < bLen - aLen; ++i)
                q = q.next;
            while (q != null) {
                if (p == q)
                    return p;
                p = p.next;
                q = q.next;
            }
            return null;
        }
    }
}

leetcode 142

题目链接
找到环形开始的index
具体思路可见代码随想录

思路:
  1. 判断是否有环——快慢指针
  2. 找到环的入口

快慢指针

  • fast每次移动2步、slow每次移动1步
  • 两者相遇——有环

找环的入口

  • 快慢指针相遇节点&head,两者同时往后
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow =head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;

            if (slow == fast) {     //有环
                ListNode p = fast;
                ListNode q = head;
                while (p != q) {
                    p = p.next;
                    q = q.next;
                }
                return p;
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值