首页 > 编程语言 >算法刷题 Day 4 | 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

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

时间:2022-12-31 16:55:07浏览次数:73  
标签:面试题 ListNode val int next 链表 节点

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

用虚拟头结点,这样会方便很多。

Tips:注意先把边界条件排除,然后注意可能会出现null的情况。

我的题解:

/**
 * 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) {
        //边界条件处理
        if(head == null || head.next == null){
            return head;
        }
        //以下都是至少有两个节点的情况了
        ListNode dummy = new ListNode(0,head);
        ListNode cur = dummy;
        while(cur.next!=null){
            ListNode one = cur.next;
            if(one.next == null){
                break;
            }
            ListNode two = one.next;
            one.next = two.next;
            two.next = one;
            cur.next = two;
            cur = one;
        }
        return dummy.next;
    }
}

题目链接/文章讲解/视频讲解: 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

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

双指针的操作,要注意,删除第N个节点,那么我们当前遍历的指针一定要指向 第N个节点的前一个节点,建议先看视频。

Tips:这道题的精髓在于定义fast和slow两个指针,并且先让fast移动n+1次,这样可以保证slow指向的是目标节点的前一个结点。

我的题解:

/**
 * 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) {
        //这里定义fast和slow两个指针
        ListNode dummy = new ListNode(0,head);
        ListNode fast = dummy;
        ListNode slow = dummy;
        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 dummy.next;
    }
}

题目链接/文章讲解/视频讲解: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

面试题 02.07. 链表相交

本题没有视频讲解,大家注意 数值相同,不代表指针相同。

Tips:注意:交点不是数值相等,而是指针相等!同时记得把交点指针初始化为null。

我的题解:

/**
 * 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 lengthA = getLength(headA);
        int lengthB = getLength(headB);
        if(lengthA >= lengthB){
            return getNode(headA,headB,lengthA,lengthB);
        }
        else{
            return getNode(headB,headA,lengthB,lengthA);
        }
    }

    public ListNode getNode(ListNode headLong, ListNode headShort, int lengthLong, int lengthShort){
        int difference = lengthLong -lengthShort;
        Boolean flag = false;
        for(int i=0;i<difference;i++){
            headLong = headLong.next;
        }
        //注意:交点不是数值相等,而是指针相等!
        ListNode start = null;
        for(int i=0;i<lengthShort;i++){
            if(flag == false){
                if(headShort == headLong){
                    flag = true;
                    start = headShort;
                }
                headShort = headShort.next;
                headLong = headLong.next;
            }
            else{
                if(headShort != headLong){
                    flag = false;
                    start = null;
                }
                headShort = headShort.next;
                headLong = headLong.next;
            }
        }
        return start;
    }

    public int getLength(ListNode head){
        int length = 0;
        while(head != null){
            length++;
            head = head.next;
        }
        return length;
    }
}

题目链接/文章讲解: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

142.环形链表II

算是链表比较有难度的题目,需要多花点时间理解 确定环和找环入口,建议先看视频。

Tips:

我的题解:

题目链接/文章讲解/视频讲解:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html

标签:面试题,ListNode,val,int,next,链表,节点
From: https://www.cnblogs.com/GavinGYM/p/17016935.html

相关文章