Skip to content

Commit ec95f50

Browse files
authored
Merge pull request #405 from thevijayshankersharma/0021-solution
Add a Solution for Merging Two Sorted Linked Lists (LeetCode Problem 21)
2 parents 4139b7a + 5bd84d4 commit ec95f50

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
id: merge-two-sorted-lists
3+
title: Merge Two Sorted Lists (LeetCode)
4+
sidebar_label: 0021-MergeTwoSortedLists
5+
tags:
6+
- Linked List
7+
- Two Pointers
8+
description: Given two sorted linked lists, merge them into a single sorted linked list.
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/merge-two-sorted-lists/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
16+
17+
### Problem Description
18+
19+
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.
20+
21+
### Examples
22+
23+
#### Example 1
24+
25+
- **Input:** list1 = [1,2,4], list2 = [1,3,4]
26+
- **Output:** [1,1,2,3,4,4]
27+
28+
#### Example 2
29+
30+
- **Input:** list1 = [], list2 = []
31+
- **Output:** []
32+
33+
#### Example 3
34+
35+
- **Input:** list1 = [], list2 = [0]
36+
- **Output:** [0]
37+
38+
### Constraints
39+
40+
- The number of nodes in both lists is in the range [0, 50].
41+
- `-100 <= Node.val <= 100`
42+
- Both `list1` and `list2` are sorted in non-decreasing order.
43+
44+
### Approach
45+
46+
To solve the problem, we can use the following approach:
47+
48+
1. **Initialize Pointers:**
49+
- Use a dummy node to form the new list and a tail pointer to keep track of the end of the merged list.
50+
51+
2. **Iterate Through the Lists:**
52+
- Compare the values of the current nodes in both lists and attach the smaller node to the merged list.
53+
- Move the pointer forward in the list from which the node was taken.
54+
55+
3. **Attach Remaining Nodes:**
56+
- Once one of the lists is exhausted, attach the remaining nodes of the other list to the merged list.
57+
58+
4. **Return Result:**
59+
- The merged list is pointed to by the dummy node's next pointer.
60+
61+
### Solution Code
62+
63+
#### Python
64+
65+
```python
66+
# Definition for singly-linked list.
67+
class ListNode:
68+
def __init__(self, val=0, next=None):
69+
self.val = val
70+
self.next = next
71+
72+
class Solution:
73+
def mergeTwoLists(self, list1, list2):
74+
# Dummy node to form the new list
75+
dummy = ListNode(0)
76+
# Tail pointer for the new list
77+
tail = dummy
78+
79+
# While both lists are non-empty, compare the values and attach the smaller node to the new list
80+
while list1 is not None and list2 is not None:
81+
if list1.val <= list2.val:
82+
tail.next = list1
83+
list1 = list1.next
84+
else:
85+
tail.next = list2
86+
list2 = list2.next
87+
tail = tail.next
88+
89+
# If either list is not empty, attach the remainder to the new list
90+
if list1 is not None:
91+
tail.next = list1
92+
else:
93+
tail.next = list2
94+
95+
# The dummy node's next pointer points to the head of the merged list
96+
return dummy.next
97+
```
98+
99+
#### C++
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
105+
// Dummy node to form the new list
106+
ListNode dummy(0);
107+
// Tail pointer for the new list
108+
ListNode* tail = &dummy;
109+
110+
// While both lists are non-empty, compare the values and attach the smaller node to the new list
111+
while (list1 != nullptr && list2 != nullptr) {
112+
if (list1->val <= list2->val) {
113+
tail->next = list1;
114+
list1 = list1->next;
115+
} else {
116+
tail->next = list2;
117+
list2 = list2->next;
118+
}
119+
tail = tail->next;
120+
}
121+
122+
// If either list is not empty, attach the remainder to the new list
123+
if (list1 != nullptr) {
124+
tail->next = list1;
125+
} else {
126+
tail->next = list2;
127+
}
128+
129+
// The dummy node's next pointer points to the head of the merged list
130+
return dummy.next;
131+
}
132+
};
133+
```
134+
135+
#### Java
136+
137+
```java
138+
class Solution {
139+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
140+
// Dummy node to form the new list
141+
ListNode dummy = new ListNode(0);
142+
// Tail pointer for the new list
143+
ListNode tail = dummy;
144+
145+
// While both lists are non-empty, compare the values and attach the smaller node to the new list
146+
while (list1 != null && list2 != null) {
147+
if (list1.val <= list2.val) {
148+
tail.next = list1;
149+
list1 = list1.next;
150+
} else {
151+
tail.next = list2;
152+
list2 = list2.next;
153+
}
154+
tail = tail.next;
155+
}
156+
157+
// If either list is not empty, attach the remainder to the new list
158+
if (list1 != null) {
159+
tail.next = list1;
160+
} else {
161+
tail.next = list2;
162+
}
163+
164+
// The dummy node's next pointer points to the head of the merged list
165+
return dummy.next;
166+
}
167+
}
168+
```
169+
170+
### Conclusion
171+
172+
The above solution efficiently merges two sorted linked lists into a single sorted linked list. It employs a straightforward approach to compare the elements from both lists and attach the smaller element to the merged list. This ensures that the merged list maintains the sorted order of the input lists, providing a simple yet effective approach to solving the problem of merging two sorted linked lists.
173+
174+

0 commit comments

Comments
 (0)