class Solution {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode dummy = new ListNode(-1, list1);
ListNode pre = dummy;
int i = 0;
// 找到a的前一个节点
while (pre != null && i < a) {
pre = pre.next;
i++;
}
// 找到b节点
ListNode node2 = pre.next;
while (node2 != null && i <= b) {
node2 = node2.next;
i++;
}
// 找到list2的最后一个节点
ListNode node3 = list2;
while (node3.next != null) {
node3 = node3.next;
}
// 拼接
node3.next = node2;
pre.next = list2;
return list1;
}
}
leetcode 1669. 合并两个链表
最新推荐文章于 2024-09-08 20:02:08 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)