Skip to content

Commit 997f49f

Browse files
chore: Merge pull request #4 from saulmaldonado/feat/merge-two-sorted-lists
merge two sorted lists
2 parents ec17bc0 + 9c3503a commit 997f49f

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Merge Two Sorted Lists
2+
3+
![difficulty](https://img.shields.io/badge/easy-5cb85c?style=for-the-badge&logoColor=white)
4+
5+
## Problem:
6+
7+
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.
8+
9+
### Example 1:
10+
11+
```
12+
Input: l1 = [1,2,4], l2 = [1,3,4]
13+
Output: [1,1,2,3,4,4]
14+
```
15+
16+
### Example 2:
17+
18+
```
19+
Input: l1 = [], l2 = []
20+
Output: []
21+
```
22+
23+
### Example 3:
24+
25+
```
26+
Input: l1 = [], l2 = [0]
27+
Output: [0]
28+
```
29+
30+
### Constraints
31+
32+
`The number of nodes in both lists is in the range [0, 50].`
33+
34+
`-100 <= Node.val <= 100`
35+
36+
`Both l1 and l2 are sorted in non-decreasing order.`
37+
38+
<details>
39+
<summary>Solutions (Click to expand)</summary>
40+
41+
### Explanation
42+
43+
Create a `sentinel` or `dummy` node to that will point to the beginning of our new linked list. Iterate through both linked lists at the same time using two different pointers comparing the value of the currently referenced nodes. Taking the smallest of the two nodes, link the node to the current node pointer of the new linked list. Continue this until one pointer reaches the end of the their list. Link the remaining part of the longer list to the end of the new list as it is already sorted. Return the new list by referencing the next node of the dummy node
44+
45+
- [JavaScript](./merge-two-sorted-lists.js)
46+
- [TypeScript](./merge-two-sorted-lists.ts)
47+
- [Java](./merge-two-sorted-lists.java)
48+
- [Go](./merge-two-sorted-lists.go)
49+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package mergetwosortedlists
2+
3+
// ListNode is a node for a Linked List
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* type ListNode struct {
12+
* Val int
13+
* Next *ListNode
14+
* }
15+
*/
16+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
17+
i := l1
18+
j := l2
19+
20+
dummyNode := &ListNode{}
21+
currentNode := dummyNode
22+
23+
for i != nil && j != nil {
24+
iVal := i.Val
25+
jVal := j.Val
26+
27+
if iVal <= jVal {
28+
currentNode.Next = i
29+
currentNode = currentNode.Next
30+
i = i.Next
31+
} else {
32+
currentNode.Next = j
33+
currentNode = currentNode.Next
34+
j = j.Next
35+
}
36+
}
37+
38+
if i != nil {
39+
currentNode.Next = i
40+
} else {
41+
currentNode.Next = j
42+
}
43+
44+
return dummyNode.Next
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(int val) {
9+
this.val = val;
10+
}
11+
12+
ListNode(int val, ListNode next) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
17+
18+
class Solution {
19+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
20+
ListNode i = l1;
21+
ListNode j = l2;
22+
ListNode dummy = new ListNode(0, l1);
23+
ListNode currentNode = dummy;
24+
25+
while (i != null && j != null) {
26+
int iValue = i.val;
27+
int jValue = j.val;
28+
29+
if (iValue <= jValue) {
30+
currentNode.next = i;
31+
currentNode = currentNode.next;
32+
i = i.next;
33+
} else {
34+
currentNode.next = j;
35+
currentNode = currentNode.next;
36+
j = j.next;
37+
}
38+
}
39+
40+
if (i != null) {
41+
currentNode.next = i;
42+
} else {
43+
currentNode.next = j;
44+
}
45+
46+
return dummy.next;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class ListNode {
2+
val;
3+
next;
4+
5+
constructor(val, next) {
6+
this.val = val;
7+
this.next = next;
8+
}
9+
}
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} l1
20+
* @param {ListNode} l2
21+
* @return {ListNode}
22+
*/
23+
function mergeTwoLists(l1, l2) {
24+
let i = l1;
25+
let j = l2;
26+
27+
const dummyNode = new ListNode();
28+
let currentNode = dummyNode;
29+
30+
while (i !== null && j !== null) {
31+
const iVal = i.val;
32+
const jVal = j.val;
33+
34+
if (iVal <= jVal) {
35+
currentNode.next = i;
36+
currentNode = currentNode.next;
37+
i = i.next;
38+
} else {
39+
currentNode.next = j;
40+
currentNode = currentNode.next;
41+
j = j.next;
42+
}
43+
}
44+
45+
if (i !== null) {
46+
currentNode.next = i;
47+
} else {
48+
currentNode.next = j;
49+
}
50+
51+
return dummyNode.next;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
5+
constructor(val?: number, next?: ListNode | null) {
6+
this.val = val ?? 0;
7+
this.next = next ?? null;
8+
}
9+
}
10+
11+
function mergeTwoLists(l1: ListNode | null, l2: ListNode | null) {
12+
let i: ListNode | null = l1;
13+
let j: ListNode | null = l2;
14+
15+
const dummyNode = new ListNode();
16+
let currentNode: ListNode | null = dummyNode;
17+
18+
while (i !== null && j !== null) {
19+
const iVal: number = i.val;
20+
const jVal: number = j.val;
21+
22+
if (iVal <= jVal) {
23+
currentNode.next = i;
24+
currentNode = currentNode.next;
25+
i = i.next;
26+
} else {
27+
currentNode.next = j;
28+
currentNode = currentNode.next;
29+
j = j.next;
30+
}
31+
}
32+
33+
if (i !== null) {
34+
currentNode.next = i;
35+
} else {
36+
currentNode.next = j;
37+
}
38+
39+
return dummyNode.next;
40+
}

0 commit comments

Comments
 (0)