代码随想录一刷day04

本文介绍了四个关于链表的操作问题,包括LeetCode的两两交换链表中的节点、删除链表的倒数第N个节点、链表相交以及环形链表II。文章详细展示了迭代和递归的解题方法,强调了双指针在解决链表问题中的重要性。
摘要由CSDN通过智能技术生成

代码随想录一刷04day


前言

链表的循环要找准结束条件和最小循环结构,先分析清楚往往事半功倍


提示:以下是本篇文章正文内容,下面案例可供参考

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

要找到最小的循环结构,保证循环可以跑起来

1.迭代写法

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

1.递归写法

/**
 * 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 firstNode = head.next;
        ListNode newNode = swapPairs(firstNode.next);
        firstNode.next = head;
        head.next = newNode;
        return firstNode;
    }
}

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

1.多遍扫描(简单)

/**
 * 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) {
        int length = 0;
        ListNode dumyNode = new ListNode(-1, head);
        ListNode p = dumyNode.next;
        while(p!=null){
            length++;
            p = p.next;
        }
        if(n>length)return head;
        int gap = length - n;
        int count = 0;
        p = dumyNode;
        while(count<gap){
            p = p.next;count++;
        }
        p.next = p.next.next;
        return dumyNode.next;
    }
}

1.一遍扫描(进阶)双指针的经典应用

设置快慢指针

/**
 * 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 dumyNode = new ListNode(-1, head);
        ListNode fast = dumyNode;
        ListNode slow = dumyNode;
        int count = 0;
        while(fast!=null){
            if(count<n){
                count++;fast= fast.next;
            }else{
                if(fast.next!=null){
                    fast = fast.next;
                    slow = slow.next;
                }else{
                    slow.next = slow.next.next;
                    return dumyNode.next;
                }
            }
        }
        return dumyNode.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) {
        int lA = 0, lB = 0, temp = 0;
        ListNode longNode, shortNode;
        ListNode dumyA = new ListNode(-1); dumyA.next = headA;
        ListNode dumyB = new ListNode(-1); dumyB.next = headB;
        ListNode pA = dumyA;
        ListNode pB = dumyB;
        while(pA.next!=null){lA++; pA = pA.next;}
        while(pB.next!=null){lB++; pB = pB.next;}
        if(lA>=lB){
            temp = lA - lB;
            longNode = dumyA;
            shortNode = dumyB;
        }else{
            temp = lB - lA;
            longNode = dumyB;
            shortNode = dumyA;
        }
        while(temp>0){temp--; longNode = longNode.next;}
        while(longNode!=null&&shortNode!=null){
            if(longNode==shortNode){
                return shortNode;
            }else{
                longNode = longNode.next;
                shortNode = shortNode.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 fastNode = head;
        ListNode slowNode = head;
        while(fastNode!=null&&fastNode.next!=null){
            fastNode = fastNode.next.next;
            slowNode = slowNode.next;
            if(fastNode==slowNode){
                ListNode index1 = fastNode;
                ListNode index2 = head;
                while(index1!=index2){
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

总结

反复练习,可以更加熟练的掌握

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值