Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 65d7f38

Browse files
committed
Add solution #1669
1 parent 2b9bfb2 commit 65d7f38

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

1669-merge-in-between-linked-lists.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1669. Merge In Between Linked Lists
3+
* https://leetcode.com/problems/merge-in-between-linked-lists/
4+
* Difficulty: Medium
5+
*
6+
* You are given two linked lists: list1 and list2 of sizes n and m respectively.
7+
*
8+
* Remove list1's nodes from the ath node to the bth node, and put list2 in their place.
9+
*
10+
* The blue edges and nodes in the following figure indicate the result:
11+
*/
12+
13+
/**
14+
* Definition for singly-linked list.
15+
* function ListNode(val, next) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.next = (next===undefined ? null : next)
18+
* }
19+
*/
20+
/**
21+
* @param {ListNode} list1
22+
* @param {number} a
23+
* @param {number} b
24+
* @param {ListNode} list2
25+
* @return {ListNode}
26+
*/
27+
var mergeInBetween = function(list1, a, b, list2) {
28+
const result = new ListNode(0, list1);
29+
30+
for (let i = 1; i < a; i++) {
31+
list1 = list1.next;
32+
}
33+
let tail = list1.next;
34+
list1.next = list2;
35+
for (let i = a; i < b; i++) {
36+
tail = tail.next;
37+
}
38+
while (list1 && list1.next) {
39+
list1 = list1.next;
40+
}
41+
list1.next = tail.next;
42+
43+
return result.next;
44+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
1566|[Detect Pattern of Length M Repeated K or More Times](./1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
240240
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
241241
1668|[Maximum Repeating Substring](./1668-maximum-repeating-substring.js)|Easy|
242+
1669|[Merge In Between Linked Lists](./1669-merge-in-between-linked-lists.js)|Medium|
242243
1672|[Richest Customer Wealth](./1672-richest-customer-wealth.js)|Easy|
243244
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
244245
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|

0 commit comments

Comments
 (0)