-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathsolution.java
103 lines (89 loc) · 3.48 KB
/
solution.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.IOException;
import java.util.Scanner;
public class MergeSortedLinkedLists {
//SinglyLinkedListNode class to represent a node
static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
//SinglyLinkedList class to represent a linked list
static class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
//Method to insert Node in the linked list
public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
//Method to merge Two Linked Lists
static SinglyLinkedListNode mergeLists(SinglyLinkedListNode a, SinglyLinkedListNode b) {
//If first linked list points to null return b
if (a == null) {
return b;
}
//If second linked list points to null return a
else if (b == null) {
return a;
}
//If data at current node of first Linked List is smaller
if (a.data < b.data) {
//Assign next of a to result of recursive call of mergeLists method on
//next node of first linked list and current node of second linked list
a.next = mergeLists(a.next, b);
return a;
} else {
//Assign next of b to result of recursive call of mergeLists method on
//current node of first linked list and next node of second linked list
b.next = mergeLists(a, b.next);
return b;
}
}
//Method to print the data of the nodes of singlyLinkedList
public static void printSinglyLinkedList(SinglyLinkedListNode node) throws IOException {
while (node != null) {
System.out.print(node.data + " -> ");
node = node.next;
}
System.out.println("NULL");
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
//Input the number of test cases
int tests = scanner.nextInt();
//For each test case
for (int testsItr = 0; testsItr < tests; testsItr++) {
//Create first Linked List
SinglyLinkedList llist1 = new SinglyLinkedList();
int llist1Count = scanner.nextInt();
for (int i = 0; i < llist1Count; i++) {
int llist1Item = scanner.nextInt();
llist1.insertNode(llist1Item);
}
//Create second Linked List
SinglyLinkedList llist2 = new SinglyLinkedList();
int llist2Count = scanner.nextInt();
for (int i = 0; i < llist2Count; i++) {
int llist2Item = scanner.nextInt();
llist2.insertNode(llist2Item);
}
//Merge the two linked lists
SinglyLinkedListNode llist3 = mergeLists(llist1.head, llist2.head);
printSinglyLinkedList(llist3);
}
scanner.close();
}
}