力扣labuladong——一刷day03

编程入门 行业动态 更新时间:2024-10-26 22:20:36

<a href=https://www.elefans.com/category/jswz/34/1766191.html style=力扣labuladong——一刷day03"/>

力扣labuladong——一刷day03

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

文章目录

  • 前言
  • 一、力扣LCR 140. 训练计划 II
  • 二、力扣LCR 142. 训练计划 IV
  • 三、力扣LCR 171. 训练计划 V
  • 四、力扣LCR 021. 删除链表的倒数第 N 个结点
  • 五、力扣LCR 022. 环形链表 II


前言


一、力扣LCR 140. 训练计划 II

/*** 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 trainingPlan(ListNode head, int cnt) {ListNode p1 = head, p2;for(int i = 0; i < cnt; i ++){p1 = p1.next;}p2 = head;while(p1 != null){p1 = p1.next;p2 = p2.next;}return p2;}
}

二、力扣LCR 142. 训练计划 IV

/*** 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 trainningPlan(ListNode l1, ListNode l2) {ListNode dump = new ListNode(-1, null), pre = dump;while(l1 != null && l2 != null){if(l1.val < l2.val){pre.next = l1;pre = pre.next;l1 = l1.next;}else{pre.next = l2;pre = pre.next;l2 = l2.next;}}if(l1 != null){pre.next = l1;}if(l2 != null){pre.next = l2;}return dump.next;}
}

三、力扣LCR 171. 训练计划 V

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
class Solution {ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode p1 = headA, p2 = headB;while(p1 != p2){if(p1 == null) p1 = headB;else p1 = p1.next;if(p2 == null) p2 = headA;else p2 = p2.next;}return p1;}
}

四、力扣LCR 021. 删除链表的倒数第 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 p = new ListNode(-1,head);ListNode r = fun(p,n+1);r.next = r.next.next;return p.next;}public ListNode fun(ListNode head, int n){ListNode p = head;int i = 1;while(i < n){i ++;p = p.next;}ListNode res = head;while(p.next != null){p = p.next;res = res.next;}return res;}
}

五、力扣LCR 022. 环形链表 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, slow = head;while(slow != null && fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;if(fast == slow){slow = head;while(slow != fast){slow = slow.next;fast = fast.next;}return fast;}}return null;}
}

更多推荐

力扣labuladong——一刷day03

本文发布于:2023-12-05 06:28:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1663388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:力扣   labuladong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!