|
| 1 | +package com.antesh.dsa.linkedlist; |
| 2 | + |
| 3 | +/** |
| 4 | + * Problem Reference: https://github.com/aniketskulkarni/coding-problems/blob/master/LeetCode/src/com/java7/problem/easy/linkedlist/MergeTwoSortedLists.java |
| 5 | + * You are given the heads of two sorted linked lists list1 and list2. |
| 6 | + * Merge the two lists in a one sorted list. The list should be made by splicing |
| 7 | + * together the nodes of the first two lists. |
| 8 | + * Return the head of the merged linked list. Duplicate nodes should be retained. |
| 9 | + * |
| 10 | + * Input: list1 = [1,2,4], list2 = [1,3,4] |
| 11 | + * Output: [1,1,2,3,4,4] |
| 12 | + * |
| 13 | + * Input: list1 = [], list2 = [] |
| 14 | + * Output: [] |
| 15 | + */ |
| 16 | +public class MergeTwoSortedList { |
| 17 | + |
| 18 | + class Node { |
| 19 | + int data; |
| 20 | + Node next; |
| 21 | + |
| 22 | + public Node(int data) { |
| 23 | + this.data = data; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public Node addToTail(Node head, int data) { |
| 28 | + if (head == null) { |
| 29 | + return new Node(data); |
| 30 | + } |
| 31 | + Node temp = new Node(data); |
| 32 | + Node ptr = head; |
| 33 | + while (ptr.next != null) { |
| 34 | + ptr = ptr.next; |
| 35 | + } |
| 36 | + |
| 37 | + ptr.next = temp; |
| 38 | + return head; |
| 39 | + } |
| 40 | + |
| 41 | + public String print(Node node) { |
| 42 | + StringBuilder sb = new StringBuilder(); |
| 43 | + Node ptr = node; |
| 44 | + while (ptr != null) { |
| 45 | + sb.append(ptr.data).append(" --> "); |
| 46 | + ptr = ptr.next; |
| 47 | + } |
| 48 | + |
| 49 | + return sb.toString(); |
| 50 | + } |
| 51 | + |
| 52 | + public Node mergeTwoSortedList(Node n1,Node n2) { |
| 53 | + if (n1 == null && n2 == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + if (n1 == null) { |
| 58 | + return n2; |
| 59 | + } |
| 60 | + |
| 61 | + if (n2 == null) { |
| 62 | + return n1; |
| 63 | + } |
| 64 | + |
| 65 | + Node mergeList = null; |
| 66 | + while (n1 != null && n2 != null) { |
| 67 | + if (n1.data < n2.data) { |
| 68 | + mergeList = addToTail(mergeList, n1.data); |
| 69 | + n1 = n1.next; |
| 70 | + } else { |
| 71 | + mergeList = addToTail(mergeList, n2.data); |
| 72 | + n2 = n2.next; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + while (n1 != null) { |
| 77 | + mergeList = addToTail(mergeList, n1.data); |
| 78 | + n1 = n1.next; |
| 79 | + } |
| 80 | + |
| 81 | + while (n2 != null) { |
| 82 | + mergeList = addToTail(mergeList, n2.data); |
| 83 | + n2 = n2.next; |
| 84 | + } |
| 85 | + |
| 86 | + return mergeList; |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String[] args) { |
| 90 | + MergeTwoSortedList list = new MergeTwoSortedList(); |
| 91 | + |
| 92 | + Node sortedList1 = null; |
| 93 | + for (int i = 1; i < 4; i++) { |
| 94 | + sortedList1 = list.addToTail(sortedList1, i); |
| 95 | + } |
| 96 | + |
| 97 | + Node sortedList2 = null; |
| 98 | + for (int i = 2; i < 5; i++) { |
| 99 | + sortedList2 = list.addToTail(sortedList2, i); |
| 100 | + } |
| 101 | + |
| 102 | + System.out.println("sortedList1 : " + list.print(sortedList1)); |
| 103 | + System.out.println("sortedList2 : " + list.print(sortedList2)); |
| 104 | + System.out.println("merged list : " + list.print(list.mergeTwoSortedList(sortedList1, sortedList2))); |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments