-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeTwoSortedLists.java
66 lines (59 loc) · 2.23 KB
/
mergeTwoSortedLists.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Definition for singly-linked list.
* public class ListNode {
* int val; // Value of the node
* ListNode next; // Reference to the next node in the linked list
* ListNode() {} // Default constructor
* ListNode(int val) { // Constructor with value parameter
* this.val = val;
* }
* ListNode(int val, ListNode next) { // Constructor with value and next node parameters
* this.val = val;
* this.next = next;
* }
* }
*/
/**
* Solution class implementing a method to merge two sorted linked lists.
*/
class Solution {
/**
* Merges two sorted linked lists into a single sorted linked list.
* @param list1 The head of the first sorted linked list.
* @param list2 The head of the second sorted linked list.
* @return The head of the merged sorted linked list.
*/
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null)
return list2;
if (list2 == null)
return list1;
ListNode ilk = new ListNode(-1); // Create a dummy node to start the merged list
ListNode temp = ilk; // Initialize a temporary pointer to the dummy node
// Iterate over both lists while they are not empty
while (list1 != null && list2 != null) {
// Compare values of the current nodes from both lists and append the smaller one to the merged list
if (list1.val <= list2.val) {
temp.next = list1;
list1 = list1.next;
} else {
temp.next = list2;
list2 = list2.next;
}
temp = temp.next; // Move the temporary pointer forward
}
// Append remaining nodes from list1 to the merged list
while(list1 != null){
temp.next = list1;
list1 = list1.next;
temp = temp.next;
}
// Append remaining nodes from list2 to the merged list
while(list2 != null){
temp.next = list2;
list2 = list2.next;
temp = temp.next;
}
return ilk.next; // Return the head of the merged list (skipping the dummy node)
}
}