|
| 1 | +package LinkedList; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class MergeSortedLinkedLists { |
| 6 | + |
| 7 | + static class ListNode { |
| 8 | + int data; |
| 9 | + ListNode next; |
| 10 | + |
| 11 | + public ListNode(int data) { |
| 12 | + this.data = data; |
| 13 | + this.next = null; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + static class SinglyLinkedList { |
| 18 | + public ListNode head; |
| 19 | + public ListNode tail; |
| 20 | + |
| 21 | + public SinglyLinkedList() { |
| 22 | + this.head = null; |
| 23 | + this.tail = null; |
| 24 | + } |
| 25 | + |
| 26 | + public void insertNode(int nodeData) { |
| 27 | + ListNode node = new ListNode(nodeData); |
| 28 | + |
| 29 | + if (this.head == null) { |
| 30 | + this.head = node; |
| 31 | + } else { |
| 32 | + this.tail.next = node; |
| 33 | + } |
| 34 | + this.tail = node; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static void printSinglyLinkedList(ListNode node) { |
| 39 | + while (node != null) { |
| 40 | + System.out.print(node.data); |
| 41 | + node = node.next; |
| 42 | + |
| 43 | + if (node != null) { |
| 44 | + System.out.print(" "); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + Given pointers to the heads of two sorted linked lists, merge them into a single sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. |
| 51 | + */ |
| 52 | + static ListNode mergeLists(ListNode head1, ListNode head2) { |
| 53 | + if(head1 == null) { |
| 54 | + return head2; |
| 55 | + } |
| 56 | + |
| 57 | + if(head2 == null) { |
| 58 | + return head1; |
| 59 | + } |
| 60 | + |
| 61 | + ListNode current1 = head1, current2 = head2; |
| 62 | + ListNode head = null, tail = null; |
| 63 | + |
| 64 | + if(current1.data <= current2.data) { |
| 65 | + head = current1; |
| 66 | + tail = current1; |
| 67 | + current1= current1.next; |
| 68 | + } |
| 69 | + else { |
| 70 | + head = current2; |
| 71 | + tail = current2; |
| 72 | + current2 = current2.next; |
| 73 | + } |
| 74 | + |
| 75 | + while(current1 != null && current2 != null) { |
| 76 | + if(current1.data <= current2.data) { |
| 77 | + tail.next = current1; |
| 78 | + tail = current1; |
| 79 | + current1 = current1.next; |
| 80 | + } else { |
| 81 | + tail.next = current2; |
| 82 | + tail = current2; |
| 83 | + current2 = current2.next; |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + if(current1 != null) { |
| 89 | + tail.next = current1; |
| 90 | + } else { |
| 91 | + tail.next = current2; |
| 92 | + } |
| 93 | + return head; |
| 94 | + } |
| 95 | + |
| 96 | + static ListNode mergeListsRecursive(ListNode head1 , ListNode head2) { |
| 97 | + |
| 98 | + if (head1 == null) { |
| 99 | + return head2; |
| 100 | + } else if (head2 == null) { |
| 101 | + return head1; |
| 102 | + } |
| 103 | + |
| 104 | + if (head1.data < head2.data) { |
| 105 | + head1.next = mergeListsRecursive(head1.next, head2); |
| 106 | + return head1; |
| 107 | + } else { |
| 108 | + head2.next = mergeListsRecursive(head1, head2.next); |
| 109 | + return head2; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public static void main(String[] args) { |
| 114 | + |
| 115 | + Scanner sc = new Scanner(System.in); |
| 116 | + int tests = sc.nextInt(); |
| 117 | + |
| 118 | + for (int testsItr = 0; testsItr < tests; testsItr++) { |
| 119 | + SinglyLinkedList list1 = new SinglyLinkedList(); |
| 120 | + |
| 121 | + int list1Count = sc.nextInt(); |
| 122 | + |
| 123 | + for (int i = 0; i < list1Count; i++) { |
| 124 | + int list1Item = sc.nextInt(); |
| 125 | + list1.insertNode(list1Item); |
| 126 | + } |
| 127 | + |
| 128 | + SinglyLinkedList list2 = new SinglyLinkedList(); |
| 129 | + |
| 130 | + int list2Count = sc.nextInt(); |
| 131 | + |
| 132 | + |
| 133 | + for (int i = 0; i < list2Count; i++) { |
| 134 | + int list2Item = sc.nextInt(); |
| 135 | + list2.insertNode(list2Item); |
| 136 | + } |
| 137 | + |
| 138 | + ListNode list3 = mergeListsRecursive(list1.head, list2.head); |
| 139 | + |
| 140 | + |
| 141 | + System.out.print("Recursive Call : "); |
| 142 | + printSinglyLinkedList(list3); |
| 143 | + |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments