代码随想录算法训练营第四天| LeetCode24 交换链表节点、LeetCode19 删除倒数第N个节点、LeetCode160 链表相交、LeetCode142 环形链表

LeetCode24 交换链表节点

题目链接:https://programmercarl.com/0024.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html代码如下:

public class code24 {
    public ListNode swapPairs(ListNode head) {

        ListNode dummyhead = new ListNode(0);
        dummyhead.next = head;
        ListNode cur = dummyhead;
        ListNode temp1 = null;
        ListNode temp2 = null;
        while (cur.next != null && cur.next.next != null) {
            // 两个临时节点防止过程中链表断开
            temp1 = cur.next;
            temp2 = cur.next.next.next;
            // 交换
            cur.next = cur.next.next;
            cur.next.next = temp1;
            temp1.next = temp2;
            cur = cur.next.next;
        }
        return dummyhead.next;
    }

注意链表操作时不要让链表断开

LeetCode19 删除倒数第N个节点

题目链接:https://programmercarl.com/0019.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.html代码如下:

public class code19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummynode = new ListNode(0);
        dummynode.next = head;
        ListNode fast = dummynode;
        ListNode slow = dummynode;

        for (int i = 0; i < n + 1; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummynode.next;

    }
}

快指针先移动n+1位置,之后一起移动,当快指针指向空指针的时候,慢指针指向倒数第n+1个元素,然后就可以删除倒数第n个元素,画图模拟一下很清晰

LeetCode160 链表相交

题目链接:https://programmercarl.com/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.html代码如下:

public class code02_07 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode p1 = headA;
        ListNode p2 = headB;

        while (p1 != p2) {

            if (p1 == null) {
                p1 = headB;
            } else {
                p1 = p1.next;
            }

            if (p2 == null) {
                p2 = headA;
            } else {
                p2 = p2.next;
            }
        }
        return p1;
    }
}

两个指针最终走过的路程相等,如果有相同节点就会相遇

LeetCode142 环形链表

题目链接:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html代码如下:

// 环形链表,主要是数学原理搞清楚
public class code142 {
    public ListNode detectCycle(ListNode head) {

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

            if (fast == slow) {
                ListNode index1 = fast;
                ListNode index2 = head;
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

文章讲的很详细,搞清楚原理写代码就很简单了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值