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

LeetCode 24 两两交换链表中的节点
题目链接:两两交换链表中的节点

思路:链表的题目一定注意画图!!!!
重点注意:链表的题目一定注意画图!!!
因为很多题目的问题逻辑是对的,但是由于对链表节点的操作顺序有问题,所以报异常。画图是最直观的解决链表题目的方法。
本题设置虚拟头结点解决此问题,值得关注的点在于一是提前保留链表中的节点,即当前不操作的链表部分先储存起来方便指针移动。
二是指针一定要保留要操作的两个节点的前驱节点,因为两个节点换位之后还要挂到前驱节点上。

package ListNode;

/**
 * @author rpstart
 * @create 2023-07-01 16:39
 */
public class swapPairs24 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(4);

        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        ListNode res = swapPairs(head);
        System.out.println(res);
    }
    public static ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1,head);
        if (head == null || head.next == null) {
            return head;
        }

        ListNode cur = dummy;
        ListNode temp = new ListNode();
        ListNode slow = new ListNode();
        ListNode fast = new ListNode();
        while (cur.next != null && cur.next.next != null) {
            temp = cur.next.next.next;
            slow = cur.next;
            fast = cur.next.next;

            cur.next = fast;
            fast.next = slow;
            slow.next = temp;

            cur = slow;
        }
        return dummy.next;
    }
}

LeetCode 19 删除链表中的倒数第N个节点
题目链接:删除链表中的第N个节点

思路:滑动窗口的题目,提前给一个滑动窗口来进行移动。

package ListNode;

import com.sun.xml.internal.bind.v2.model.core.ID;

/**
 * @author rpstart
 * @create 2023-07-01 17:08
 */
public class removeNthFromEnd19 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
//        ListNode node1 = new ListNode(2);
//        ListNode node2 = new ListNode(3);
//        ListNode node3 = new ListNode(4);
//        ListNode node4 = new ListNode(5);

//        head.next = node1;
//        node1.next = node2;
//        node2.next = node3;
//        node3.next = node4;
        ListNode res = removeNthFromEnd(head, 1);
        System.out.println(res);
    }
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1,head);
        ListNode slowIndex = dummy;
        ListNode fastIndex = dummy;
        int temp = n;
        while (n != 0) {
            fastIndex = fastIndex.next;
            n--;
        }

        while (fastIndex.next != null) {
            slowIndex = slowIndex.next;
            fastIndex = fastIndex.next;
        }
        slowIndex.next = slowIndex.next.next;
        return dummy.next;
    }

}

LeetCode 160 相交链表
题目链接:相交链表

思路:思路比较简单,既然是比较两个链表中的相同位置,由于链表中的节点是对象,所以引用地址必定相同。先处理两个链表的长短关系,然后两个指针分别移动到两个链表的形同位置进行判断即可。

package ListNode;

/**
 * @author rpstart
 * @create 2023-07-01 17:50
 */
public class getIntersectionNode160 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(9);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(4);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        ListNode headB = new ListNode(3);
        headB.next = node3;


        ListNode res = getIntersectionNode(head, headB);
        System.out.println(res);

    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int sizeA = 0;
        int sizeB = 0;
        ListNode curA = headA;
        while (curA != null) {
            curA = curA.next;
            sizeA++;
        }
        ListNode curB = headB;
        while (curB != null) {
            curB = curB.next;
            sizeB++;
        }

        if (sizeA > sizeB) {
            ListNode temp = headB;
            headB = headA;
            headA = temp;
            int tempSize = sizeB;
            sizeB = sizeA;
            sizeA = tempSize;
        }
        curA = headA;
        curB = headB;
        int index = sizeB - sizeA;
        for (int i = 0; i < index; i++) {
            curB = curB.next;
        }

        while (curA != null && curB != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}

LeetCode 142 环形链表2
题目链接:环形链表2

思路:环形链表最终的解决思路有两个,一是判断是否有环,通过快慢指针;二是怎么找环的位置,一定要注意的是第一次相交的位置不一定是环的起点,可能相交在环的内部。

package ListNode;

/**
 * @author rpstart
 * @create 2023-07-01 19:55
 */
public class detectCycle142 {
    public static void main(String[] args) {
        ListNode head = new ListNode(3);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(0);
        ListNode node3 = new ListNode(-4);

        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node1;

        ListNode res = detectCycle(head);
        System.out.println(res.val);
    }
    public static ListNode detectCycle(ListNode head) {

        ListNode slowIndex = head;
        ListNode fastIndex = head;
        while (fastIndex != null && fastIndex.next != null) {
            slowIndex = slowIndex.next;
            fastIndex = fastIndex.next.next;

            if (slowIndex == fastIndex) {
                ListNode index1 = fastIndex;
                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、付费专栏及课程。

余额充值