Skip to content

Commit 89bcd59

Browse files
committed
Add merge two sorted lists solution
1 parent 77a7248 commit 89bcd59

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package java1.algorithms.linkedlist;
2+
3+
public class MergeTwoSortedLists {
4+
//TC: O(m+n) SC:O(1)
5+
private static Node mergeSortedLists(Node l1, Node l2) {
6+
Node newHead = new Node(-1);
7+
Node p1 = l1, p2 = l2, currNode = newHead;
8+
while(p1 != null & p2 != null) {
9+
if(p1.value < p2.value) {
10+
currNode.next = p1;
11+
p1 = p1.next;
12+
} else {
13+
currNode.next = p2;
14+
p2 = p2.next;
15+
}
16+
currNode = currNode.next;
17+
}
18+
if(p1 == null) {
19+
currNode.next = p2;
20+
}
21+
if(p2 == null) {
22+
currNode.next = p1;
23+
}
24+
return newHead.next;
25+
}
26+
27+
//Recursive approach
28+
private static Node mergeSortedListsRecursion(Node l1, Node l2) {
29+
if(l1 == null) return l2;
30+
if(l2 == null) return l1;
31+
32+
if(l1.value < l2.value) {
33+
l1.next = mergeSortedListsRecursion(l1.next, l2);
34+
return l1;
35+
} else {
36+
l2.next = mergeSortedListsRecursion(l1, l2.next);
37+
return l2;
38+
}
39+
}
40+
41+
private static void printLinkedlist(Node head) {
42+
while(head != null) {
43+
System.out.println(head.value + " ");
44+
head = head.next;
45+
}
46+
}
47+
public static void main(String[] args) {
48+
Node headNode1 = new Node(10);
49+
Node node2 = new Node(20);
50+
Node node3 = new Node(30);
51+
Node node4 = new Node(40);
52+
Node node5 = new Node(50);
53+
Node node6 = new Node(60);
54+
55+
Node headNode2 = new Node(5);
56+
Node node7 = new Node(15);
57+
Node node8 = new Node(25);
58+
Node node9 = new Node(35);
59+
Node node10 = new Node(45);
60+
Node node11 = new Node(55);
61+
62+
63+
headNode1.setNext(node2);
64+
node2.setNext(node3);
65+
node3.setNext(node4);
66+
node4.setNext(node5);
67+
node5.setNext(node6);
68+
69+
headNode2.setNext(node7);
70+
node7.setNext(node8);
71+
node8.setNext(node9);
72+
node9.setNext(node10);
73+
node10.setNext(node11);
74+
75+
printLinkedlist(mergeSortedLists(headNode1, headNode2));
76+
printLinkedlist(mergeSortedListsRecursion(headNode1, headNode2));
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
}
6+
7+
setNext(next) {
8+
this.next = next;
9+
}
10+
}
11+
12+
//TC: O(m+n) SC:O(1)
13+
function mergeTwoSortedLists(l1, l2) {
14+
let newHead = new Node(-1);
15+
let currNode = newHead;
16+
17+
while(l1 != null && l2 != null) {
18+
if(l1.value < l2.value) {
19+
currNode.next = l1;
20+
l1 = l1.next;
21+
} else {
22+
currNode.next = l2;
23+
l2 = l2.next;
24+
}
25+
currNode = currNode.next;
26+
}
27+
return newHead.next;
28+
}
29+
30+
//Recursion approach: TC: O(m+n) SC:O(m+n)
31+
function mergeTwoSortedListsRecursion(l1, l2){
32+
if(l1 == null) return l2;
33+
if(l2 == null) return l1;
34+
35+
if(l1.value < l2.value) {
36+
l1.next = mergeTwoSortedListsRecursion(l1.next, l2);
37+
return l1;
38+
} else {
39+
l2.next = mergeTwoSortedListsRecursion(l1, l2.next);
40+
return l2;
41+
}
42+
}
43+
44+
function printLinkedList(head) {
45+
let temp = head;
46+
while(temp != null) {
47+
console.log(temp.value + " ");
48+
temp = temp.next;
49+
}
50+
51+
}
52+
53+
let headNode1 = new Node(10);
54+
let node2 = new Node(20);
55+
let node3 = new Node(30);
56+
let node4 = new Node(40);
57+
let node5 = new Node(50);
58+
let node6 = new Node(60);
59+
60+
let headNode2 = new Node(5);
61+
let node7 = new Node(15);
62+
let node8 = new Node(25);
63+
let node9 = new Node(35);
64+
let node10 = new Node(45);
65+
let node11 = new Node(55);
66+
67+
68+
headNode1.setNext(node2);
69+
node2.setNext(node3);
70+
node3.setNext(node4);
71+
node4.setNext(node5);
72+
node5.setNext(node6);
73+
74+
headNode2.setNext(node7);
75+
node7.setNext(node8);
76+
node8.setNext(node9);
77+
node9.setNext(node10);
78+
node10.setNext(node11);
79+
80+
printLinkedList(mergeTwoSortedLists(headNode1, headNode2));
81+
printLinkedList(mergeTwoSortedListsRecursion(headNode1, headNode2));

0 commit comments

Comments
 (0)