代码随想录算法训练营Day4—— 24两两交换链表中的节点、19删除链表的倒数第N个节点、160相交链表、142环形链表II

文章介绍了四个关于链表的编程问题:两两交换链表中的节点、删除链表的倒数第N个节点、寻找相交链表的节点以及检测环形链表的入口。每个问题都提供了思路分析、Java代码实现及时间、空间复杂度的总结,强调了使用虚拟头节点和基本链表操作的重要性。
摘要由CSDN通过智能技术生成

4-1.24题两两交换链表中的节点

题目

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

思路

考查链表操作

代码

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyhead = new ListNode(-1);
        dummyhead.next = head;
        ListNode cur = dummyhead;
        ListNode tmp;
        ListNode firstnode;
        ListNode secondnode;

        while (cur.next != null && cur.next.next!= null) {
            tmp = cur.next.next.next;
            firstnode = cur.next;
            secondnode = cur.next.next;
            cur.next = secondnode;    //步骤1
            secondnode.next = firstnode;    //步骤2
            firstnode.next = tmp;    //步骤3
            cur = firstnode;    //下一轮交换
        }
        return dummyhead.next;
    }
}

总结

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

4-2.19题删除链表的倒数第N个节点

题目

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

思路

考查链表的删除操作

代码

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyhead = new ListNode(-1);
        dummyhead.next = head;
        ListNode fast = dummyhead;
        ListNode slow = dummyhead;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast.next != null) {
            fast= fast.next;
            slow = slow.next;
        }
        //slow所指的位置就是待删除元素的前一个位置
        slow.next = slow.next.next;
        return dummyhead.next;
    }
}

总结

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

4-3.160题相交链表

题目

160题相交链表

思路

值相同不代表链表相交

代码

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的长度
            lenA++;
            curA = curA.next;
        }
        while (curB != null) { // 求链表B的长度
            lenB++;
            curB = curB.next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头,lenA为其长度
        if (lenB > lenA) {
            //1. swap (lenA, lenB);
            int tmpLen = lenA;
            lenA = lenB;
            lenB = tmpLen;
            //2. swap (curA, curB);
            ListNode tmpNode = curA;
            curA = curB;
            curB = tmpNode;
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while (gap-- > 0) {
            curA = curA.next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;   
    }
}

总结

时间复杂度 : O ( n + m ) O(n+m) O(n+m)
空间复杂度 : O ( 1 ) O(1) O(1)

4-4.142题环形链表II

题目

142题环形链表II

思路

一是判断链表是否有环
二是找到环内入口

代码

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 indexhead = head;
                ListNode indexmeet = slow;
                while (indexhead != indexmeet) {
                    indexhead =indexhead.next;
                    indexmeet = indexmeet.next;
                }
                return indexmeet;
            }
        }
        return null;
    }
}

总结

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

链表总结

虚拟头节点YYDS
• 链表的基本操作
• 反转链表
• 相交链表
• 环形链表

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值