Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Java/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 33 additions & 20 deletions Java/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package problems.leetcode.linked_list;

import problems.leetcode.utils.ListNode;

public class MergeTwoSortedLists {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode sortedList = new ListNode();
if (list1 == null)
return list2;
if (list2 == null)
return list1;
if (list1.val < list2.val){
sortedList = list1;
list1 = list1.next;
} else {
sortedList = list2;
list2 = list2.next;
}
sortedList.next = mergeTwoLists(list1, list2);
return sortedList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package problems.leetcode.linked_list;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import problems.leetcode.utils.ListNode;

import static org.junit.jupiter.api.Assertions.*;

public class MergeTwoSortedListsTest {

private MergeTwoSortedLists solution;

@BeforeEach
public void setUp() {
solution = new MergeTwoSortedLists();
}

@Test
public void testMergeTwoListsBothEmpty() {
ListNode l1 = null;
ListNode l2 = null;
ListNode merged = solution.mergeTwoLists(l1, l2);
assertNull(merged);
}

@Test
public void testMergeTwoListsWithOneEmpty() {
ListNode l1 = new ListNode(1);
ListNode l2 = null;
ListNode merged = solution.mergeTwoLists(l1, l2);
assertEquals(l1, merged);

l1 = null;
l2 = new ListNode(2);
merged = solution.mergeTwoLists(l1, l2);
assertEquals(l2, merged);
}

@Test
public void testMergeTwoListsBothNonEmpty() {
// l1: 1 -> 2 -> 4
ListNode l1 = new ListNode(1);
l1.next = new ListNode(2);
l1.next.next = new ListNode(4);

// l2: 1 -> 3 -> 4
ListNode l2 = new ListNode(1);
l2.next = new ListNode(3);
l2.next.next = new ListNode(4);

// Expected merged list: 1 -> 1 -> 2 -> 3 -> 4 -> 4
ListNode expected = new ListNode(1);
expected.next = new ListNode(1);
expected.next.next = new ListNode(2);
expected.next.next.next = new ListNode(3);
expected.next.next.next.next = new ListNode(4);
expected.next.next.next.next.next = new ListNode(4);

ListNode merged = solution.mergeTwoLists(l1, l2);
assertTrue(areListsEqual(expected, merged));
}

// Helper method to check if two linked lists are equal
private boolean areListsEqual(ListNode l1, ListNode l2) {
while (l1 != null && l2 != null) {
if (l1.val != l2.val) {
return false;
}
l1 = l1.next;
l2 = l2.next;
}
return l1 == null && l2 == null;
}
}

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Here is a list of the problems I've solved along with links to the corresponding
| [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/GroupAnagrams.java) |
| [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/ProductExceptSelf.java) |
| [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Java](Java/src/main/java/problems/leetcode/arrays_hashing/TopKFrequent.java) |
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Java](Java/src/main/java/problems/leetcode/linked_list/MergeTwoSortedLists.java) |
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Java](Java/src/main/java/problems/leetcode/linked_list/ReverseLinkedList.java) |
| [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | [Java](Java/src/main/java/problems/leetcode/stack/DailyTemperatures.java) |
| [Min Stack](https://leetcode.com/problems/min-stack/) | [Java](Java/src/main/java/problems/leetcode/stack/MinStack.java) |
Expand Down
54 changes: 54 additions & 0 deletions docs/Linked List/0021. Merge Two Sorted Lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Merge Two Sorted Lists

## Intuition

When merging two sorted linked lists, my first thought was a recursive approach. The idea is to compare the values of the current nodes in both lists and select the smaller one to be the next node in the merged list and set repace the current node in that list, by the next one. This process continues recursively until one of the input lists is null/empty.

## Approach

1. Create a new `ListNode` called `sortedList` to keep track of the merged list.
2. Check if either of the input lists is empty. If one of them is empty, return the other list because the merged list would be the non-empty list.
3. Compare the values of the current nodes in `list1.val` and `list2.val`.
4. Select the node with the smaller value to be the next node in `sortedList`, and advance the pointer of the corresponding list by setting it to `.next`.
5. Recursively call the `mergeTwoLists` function with the updated lists.
6. Continue this process until either one of `list1` or `list2` are exhausted.
7. Return the `sortedList`, which now contains the merged sorted linked list.

## Complexity

- Time complexity: $$O(n)$$ Where $n$ is the total number of nodes in the two input lists. This is because we visit each node once during the merge process.
- Space complexity: $$O(n)$$ Where $n$ is the space required for the recursive call stack. In the worst case, the stack depth is proportional to the number of nodes in the input lists.

## Code

```java
/**
* 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 mergeTwoLists(ListNode list1, ListNode list2) {
ListNode sortedList = new ListNode();
if (list1 == null)
return list2;
if (list2 == null)
return list1;
if (list1.val < list2.val){
sortedList = list1;
list1 = list1.next;
} else {
sortedList = list2;
list2 = list2.next;
}
sortedList.next = mergeTwoLists(list1, list2);
return sortedList;
}
}
```