Day 4 | 链表 24. 两两交换链表中的节点 、19.删除链表的倒数第N个节点 、 面试题 02.07. 链表相交 、142.环形链表II 、 总结

24. 两两交换链表中的节点

题目
文章讲解
视频讲解
思路:

  • 只要有断的位置就应该提前设置缓存量,将断点保存,还要注意要在循环里面设置
  • ListNode dummy = new ListNode(0);
    dummy.next = head;//定义虚拟头节点两句缺一不可!
/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {//奇数和偶数情况的终止条件,顺序不能反,有先后关系
            ListNode temp0 = cur.next;
            ListNode temp1 = cur.next.next.next;//有断点的位置一定要提前设置临时缓存,还要注意要在循环里面设置
            cur.next = cur.next.next;
            cur.next.next = temp0;
            temp0.next = temp1;
            cur = cur.next.next;
        }
        return dummy.next;
    }
}

19.删除链表的倒数第N个节点 10:38-11:06

题目
文章讲解
视频讲解
思路:

  • 快慢指针,快指针先走n步,随后快慢一起走,但是需要注意:删除元素必须找到该元素的上一个元素,因此快指针应先走n+1步
  • return 要在pubilc{}里面
/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        for (int i = 0; i <= n; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }

}

面试题 02.07链表相交(题目绕蒙了)

题目
文章讲解
无视频讲解
思路:求两个链表交点节点的指针。注意,交点不是数值相等,而是指针相等。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
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) {
            lenB++;
            curB = curB.next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头,lenA为其长度
        if (lenB > lenA) {
            // 交换lenA与lenB
            int tmplen = lenA;
            lenA = lenB;
            lenB = tmplen;
            // 交换curA与curB
            ListNode tmpNode = curA;
            curA = curB;
            curB = tmpNode;
        }
        // 求长度
        int gap = lenA - lenB;
        // 让curA与curB在同一起点
        for (int i = 0; i < gap; i++) {
            curA = curA.next;
        }
        // 遍历curA与curB,遇到相同直接返回
        while (curA != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}

142.环形链表II

题目
文章讲解
视频讲解
思路:
在这里插入图片描述
slow=x+y
fast=x+y+n(y+z)
2slow=fast
当n=1,x=z

/**
 * Definition for singly-linked list.
 * class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {//只检查fast就可以了
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                ListNode index1 = fast;
                ListNode index2 = head;
                while (index1 != index2) {//x=z
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值