Re: [閒聊] 每日leetcode

作者: hduek153 (專業打醬油)   2024-03-20 11:46:31
突然心神不寧捏
是不是不妙
快三個月沒刷題了感覺要複習了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;
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com