代码随想录二刷day04

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣24. 两两交换链表中的节点](#一、力扣24. 两两交换链表中的节点)
  • [二、力扣19. 删除链表的倒数第 N 个结点](#二、力扣19. 删除链表的倒数第 N 个结点)
  • [三、力扣面试题 02.07. 链表相交](#三、力扣面试题 02.07. 链表相交)
  • [四、力扣142. 环形链表 II](#四、力扣142. 环形链表 II)

前言


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

迭代

java 复制代码
/**
 * 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 node = new ListNode(-1, head);
        ListNode prev = node, cur = head;
        int temp;
        while(cur != null && cur.next != null){
            temp = cur.val;
            cur.val = cur.next.val;
            cur.next.val = temp;
            prev = cur.next;
            cur = prev.next;
        }
        return node.next;
    }
}

递归

java 复制代码
/**
 * 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;
        }
        int temp;
        ListNode cur = head;
        temp = cur.val;
        cur.val = cur.next.val;
        cur.next.val = temp;
        ListNode node = swapPairs(cur.next.next);
        if(node == null){
            return cur;
        }
        return cur;
    }
}

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

直观法

java 复制代码
/**
 * 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) {
        if(head == null){
            return head;
        }
        ListNode node = new ListNode(-1, head);
        ListNode pre = node, p = head;
        int i = 0, size = 0;
        while(p != null){
            size ++;
            p = p.next;
        }
        if(n > size){
            return head;
        }
        p = head;
        while(i < size-n){
            i ++;
            pre = p;
            p = p.next;
        }
        pre.next = p.next;
        return node.next;
    }
}

双指针法

java 复制代码
/**
 * 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 node = new ListNode(-1, head);
        ListNode fast = node, slow = node;
        int i = 1;
        while(i <= n+1 && fast != null){
            i ++;
            fast = fast.next;
        }
        if(fast == null && i < n+1){
            return node.next;
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return node.next;
    }
}

三、力扣面试题 02.07. 链表相交

java 复制代码
/**
 * 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 longL = new ListNode(-1, headA);
        ListNode shortL = new ListNode(-1, headB);
        ListNode pa = headA, pb = headB;
        int lenA = 0, lenB = 0, diff = 0;
        while(pa != null){
            lenA ++;
            pa = pa.next;
        }
        while(pb != null){
            lenB ++;
            pb = pb.next;
        }
        if(lenA > lenB){
            diff = lenA - lenB;
            longL.next = headA;
            shortL.next = headB;
        }else{
            diff = lenB - lenA;
            longL.next = headB;
            shortL.next = headA;
        }
        pa = longL.next; pb = shortL.next;
        for(int i = 0; i < diff; i++){
            pa = pa.next;
        }
        while(pa != null && pb != null){
            if(pa == pb){
                return pa;
            }
            pa = pa.next;
            pb = pb.next;
        }
        return null;
    }
}

四、力扣142. 环形链表 II

因为快指针的速度是慢指针的两倍, 所以,在进入环形之后, 在快慢指针相遇之前, 慢指针必然走不满一圈,设入环之前的节点数为x, 入环后到相遇节点之前为y个, 相遇节点到入环处为z个, 相遇时, 快指针走过的节点数是慢指针的2倍, 故有,2(x + y) = x + n(z + y) + y,, 化简后为, x = n(z +y)-y, 右边提取一个 y 得到,x = (n-1)(z+y) + z, 此时的慢指针,一定会先走完z, 然后就是n-1圈, 就一直在进入点等待x

java 复制代码
/**
 * 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, slowNode = head;
        while(fastNode != null && fastNode.next != null){
            fastNode = fastNode.next.next;
            slowNode = slowNode.next;
            if(fastNode == slowNode){
                ListNode index1 = head;
                ListNode index2 = slowNode;
                while(index1 != index2){
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}
相关推荐
haoly19891 分钟前
数据结构和算法篇-线性查找优化-移至开头策略
数据结构·算法·移至开头策略
瓯雅爱分享6 分钟前
Java+Vue构建的采购招投标一体化管理系统,集成招标计划、投标审核、在线竞价、中标公示及合同跟踪功能,附完整源码,助力企业实现采购全流程自动化与规范化
java·mysql·vue·软件工程·源代码管理
mit6.8242 小时前
[C# starter-kit] 命令/查询职责分离CQRS | MediatR |
java·数据库·c#
诸神缄默不语3 小时前
Maven用户设置文件(settings.xml)配置指南
xml·java·maven
任子菲阳3 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
学Linux的语莫3 小时前
机器学习数据处理
java·算法·机器学习
找不到、了3 小时前
JVM的即时编译JIT的介绍
java·jvm
earthzhang20214 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
西瓜er4 小时前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg
你总是一副不开心的样子(´ . .̫ .4 小时前
一、十天速通Java面试(第三天)
java·面试·职场和发展·java面试