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

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

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

题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/
思路:使用头结点然后画图

/**
 * 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 dy = new ListNode(-1, head);
        ListNode cur = dy, first, second, temp;
        while (cur.next != null && cur.next.next != null) {
            temp = cur.next.next.next;
            first = cur.next;
            second = cur.next.next;
            cur.next = second;
            second.next = first;
            first.next = temp;
            cur = first;
        }
        return dy.next;
    }
}

二、19.删除链表的倒数第N个节点

题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
思路:要删除倒数第几个,就用双指针维护一个间距,间距和倒数的n相等后就同步往下走,直至结束,慢指针的next就是要删除的元素。

/**
 * 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 dy = new ListNode(-1, head);
        ListNode slow = dy, fast = dy;
        int len = 0;
        while (fast != null && fast.next != null) {
            if (len < n) {
                fast = fast.next;
                len++;
            } else {
                slow = slow.next;
                fast = fast.next;
            }
        }
        slow.next = slow.next.next;
        return dy.next;
    }
}

三、面试题 02.07. 链表相交

题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/
思路:求相交只需要尾部对齐即可,求两链表长度,长的往前走到短的长度,然后同步走判断即可。

/**
 * 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) {
        int lenA = 0, lenB = 0;
        ListNode dy1 = new ListNode(-1, headA);
        ListNode dy2 = new ListNode(-1, headB);
        ListNode p1 = dy1, p2 = dy2;
        while (p1.next != null) {
            lenA++;
            p1 = p1.next;
        }
        while (p2.next != null) {
            lenB++;
            p2 = p2.next;
        }
        p1 = dy1;
        p2 = dy2;
        while (lenA < lenB) {
            lenB--;
            p2 = p2.next;
            
        }
        while (lenA > lenB) {
            lenA--;
            p1 = p1.next;
        }
        while (p1.next != null && p2.next != null) {
            if (p1.next == p2.next) {
                break;
            }
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1.next;
    }
}

四、142.环形链表II

题目链接:https://leetcode.cn/problems/linked-list-cycle-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 fast = head, slow = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                ListNode p1 = head, p2 = fast;
                while (p1 != p2) {
                    p1 = p1.next;
                    p2 = p2.next;
                }
                return p1;
            }
        }
        return null;
    }
}

你可能感兴趣的:(力扣算法题,链表,数据结构)