Re: [閒聊] 每日leetcode

看板Marginalman作者 (老天保佑)時間1月前 (2024/03/20 11:46), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串65/169 (看更多)
突然心神不寧捏 是不是不妙 快三個月沒刷題了感覺要複習了QQ 直接偷懶隨便寫 https://leetcode.com/problems/merge-in-between-linked-lists /** * 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 mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode pre = new ListNode(); pre.next = list1; ListNode cur = list1; for (int i = 0; i < a; i ++) { cur = cur.next; pre = pre.next; } for (int i = 0; i < b - a; i++) { cur = cur.next; } ListNode tail = cur.next; cur.next = null; pre.next = list2; cur = list2; while (cur != null && cur.next != null) { cur = cur.next; } cur.next = tail; return list1; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 76.32.119.228 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1710906393.A.C6D.html
文章代碼(AID): #1b-bmPnj (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1b-bmPnj (Marginalman)