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

本文介绍了如何使用虚拟头节点实现两两交换链表节点、删除链表倒数第N个节点的方法,以及链表相交和环形链表的检测。通过递归和快慢指针策略,展示了链表操作的基本技巧。
摘要由CSDN通过智能技术生成

系列文章目录



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

①虚拟头节点法

/**
 * 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 dummyHead = new ListNode(-1);// 设置一个虚拟头结点
        dummyHead.next = head;// 将虚拟头结点指向head,这样方便后面做删除操作
        ListNode pre = dummyHead;
        ListNode temp;// 临时节点,保存两个节点后面的节点
        ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点
        ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点
        while (pre.next!=null&&pre.next.next!=null){//先后顺序不能变
            firstnode=pre.next;
            secondnode=pre.next.next;
            temp=pre.next.next.next;
            pre.next=secondnode;
            secondnode.next=firstnode;
            firstnode.next=temp;
            pre=firstnode;// cur移动,准备下一轮交换
        }
        return dummyHead.next;
    }
}

在这里插入图片描述

②递归法以后有时间看


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

虚拟头节点+快慢指针

/**
 * 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 dummyHead = new ListNode(-1);
        dummyHead.next=head;
        ListNode fast = dummyHead;
        ListNode slow = dummyHead;
        n++;
        while (n-- > 0) {
            fast = fast.next;
        }
/*        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }*/
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        //此时 slow 的位置就是待删除元素的前一个位置。
        slow.next = slow.next.next;
        return dummyHead.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) {
        ListNode curA = headA;
        ListNode curB = headB;
        int lenA = 0, lenB = 0;
        while (curA != null) {// 求链表A的长度
            curA = curA.next;
            lenA++;
        }
        while (curB != null) { // 求链表B的长度
            lenB++;
            curB = curB.next;
        }
        //注意:遍历完链表长度后curA和curB已指向链表末尾,需重置将两个指针指向链表的头节点
        curA=headA;
        curB=headB;
        // 让curA为最长链表的头,lenA为其长度
        if (lenB > lenA) {
            ListNode tempNode = curA;
            curA = curB;
            curB = tempNode;
            int tmpLen = lenA;
            lenA = lenB;
            lenB = tmpLen;
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        for (int i = 0; i < gap; i++) {
            curA = curA.next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != null) {
            if(curA==curB){
                return curA;
            }
            curA=curA.next;
            curB=curB.next;
        }
        return null;
    }
}

在这里插入图片描述


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 fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {//fast指针比slow指针快,故判断fast指针即可
            fast = fast.next.next;
            slow = slow.next;
            // 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
            if (fast == slow) {// 有环
                ListNode index1 = head;
                ListNode index2 = fast;
                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;// 返回环的入口
            }
        }
        return null;
    }
}

在这里插入图片描述


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值