代码随想录二刷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;
    }
}
相关推荐
float_六七7 分钟前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天1 小时前
JAVA stream().flatMap()
java·windows
焦耳加热2 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn2 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
颜如玉2 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
u6062 小时前
常用排序算法核心知识点梳理
算法·排序
程序员的世界你不懂3 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年3 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152874 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草4 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器