代码随想录算法训练营第四天 24.两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/)
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

**示例 1:**

![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)

**输入:**head = [1,2,3,4]
**输出:**[2,1,4,3]

**示例 2:**

**输入:**head = []
**输出:**[]

**示例 3:**

**输入:**head = [1]
**输出:**[1]

代码:
/**

 * 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 dumyhead = new ListNode(-1); // 设置一个虚拟头结点

        dumyhead.next = head; // 将虚拟头结点指向head,这样方便后面做删除操作

        ListNode cur = dumyhead;

        ListNode temp; // 临时节点,保存两个节点后面的节点

        ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点

        ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点

        while (cur.next != null && cur.next.next != null) {

            temp = cur.next.next.next;

            firstnode = cur.next;

            secondnode = cur.next.next;

            cur.next = secondnode;       // 步骤一

            secondnode.next = firstnode; // 步骤二

            firstnode.next = temp;      // 步骤三

            cur = firstnode; // cur移动,准备下一轮交换

        }

        return dumyhead.next;  

    }

}

[19. 删除链表的倒数第 N 个结点](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)
给你一个链表,删除链表的倒数第 `n` 个结点,并且返回链表的头结点。

**示例 1:**

![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)

**输入:**head = [1,2,3,4,5], n = 2
**输出:**[1,2,3,5]

**示例 2:**

**输入:**head = [1], n = 1
**输出:**[]

**示例 3:**

**输入:**head = [1,2], n = 1
**输出:**[1]

代码:
/**

 * 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 dummyNode = new ListNode(0);

        dummyNode.next = head;

  

        ListNode fastIndex = dummyNode;

        ListNode slowIndex = dummyNode;

  

        // 只要快慢指针相差 n 个结点即可

        for (int i = 0; i < n  ; i++){

            fastIndex = fastIndex.next;

        }

  

        while (fastIndex != null){

            fastIndex = fastIndex.next;

            slowIndex = slowIndex.next;

        }

        slowIndex.next = slowIndex.next.next;

        return dummyNode.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) {

        // p1 指向 A 链表头结点,p2 指向 B 链表头结点

        ListNode p1 = headA, p2 = headB;

        while (p1 != p2) {

            // p1 走一步,如果走到 A 链表末尾,转到 B 链表

            if (p1 == null) p1 = headB;

            else            p1 = p1.next;

            // p2 走一步,如果走到 B 链表末尾,转到 A 链表

            if (p2 == null) p2 = headA;

            else            p2 = p2.next;

        }

        return p1;

    }

}

###  142.环形链表II
/**

 * 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 slow = head;

        ListNode fast = head;

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

            slow = slow.next;

            fast = fast.next.next;

            if (slow == fast) {// 有环

                ListNode index1 = fast;

                ListNode index2 = head;

                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口

                while (index1 != index2) {

                    index1 = index1.next;

                    index2 = index2.next;

                }

                return index1;

            }

        }

        return null;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值