Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIJava Program For Merging Two Sorted Linked Lists Such That Merged List...

Java Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order

Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order).

Examples: 

Input:  a: 5->10->15->40
        b: 2->3->20 
Output: res: 40->20->15->10->5->3->2

Input:  a: NULL
        b: 2->3->20 
Output: res: 20->3->2

A Simple Solution is to do following. 
1) Reverse first list ‘a’
2) Reverse second list ‘b’
3) Merge two reversed lists.
Another Simple Solution is first Merge both lists, then reverse the merged list.
Both of the above solutions require two traversals of linked list. 

How to solve without reverse, O(1) auxiliary space (in-place) and only one traversal of both lists? 
The idea is to follow merge style process. Initialize result list as empty. Traverse both lists from beginning to end. Compare current nodes of both lists and insert smaller of two at the beginning of the result list. 

1) Initialize result list as empty: res = NULL.
2) Let 'a' and 'b' be heads first and second lists respectively.
3) While (a != NULL and b != NULL)
    a) Find the smaller of two (Current 'a' and 'b')
    b) Insert the smaller value node at the front of result.
    c) Move ahead in the list of smaller node. 
4) If 'b' becomes NULL before 'a', insert all nodes of 'a' 
   into result list at the beginning.
5) If 'a' becomes NULL before 'b', insert all nodes of 'a' 
   into result list at the beginning. 

Below is the implementation of above solution.

Java




<div id="highlighter_344288" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// Java program to merge two sorted </code></div><div class="line number2 index1 alt1"><code class="comments">// linked list such that merged </code></div><div class="line number3 index2 alt2"><code class="comments">// list is in reverse order</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="comments">// Linked List Class</code></div><div class="line number6 index5 alt1"><code class="keyword">class</code> <code class="plain">LinkedList </code></div><div class="line number7 index6 alt2"><code class="plain">{</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="comments">// Head of list</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="plain">Node head;  </code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="plain">Node a, b;</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="comments">// Node Class </code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="keyword">static</code> <code class="keyword">class</code> <code class="plain">Node </code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="keyword">int</code> <code class="plain">data;</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">        </code><code class="plain">Node next;</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="comments">// Constructor to create</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="comments">// a new node</code></div><div class="line number20 index19 alt1"><code class="undefined spaces">        </code><code class="plain">Node(</code><code class="keyword">int</code> <code class="plain">d) </code></div><div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">            </code><code class="plain">data = d;</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">            </code><code class="plain">next = </code><code class="keyword">null</code><code class="plain">;</code></div><div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number26 index25 alt1"> </div><div class="line number27 index26 alt2"><code class="undefined spaces">    </code><code class="keyword">void</code> <code class="plain">printlist(Node node) </code></div><div class="line number28 index27 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number29 index28 alt2"><code class="undefined spaces">        </code><code class="keyword">while</code> <code class="plain">(node != </code><code class="keyword">null</code><code class="plain">) </code></div><div class="line number30 index29 alt1"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number31 index30 alt2"><code class="undefined spaces">            </code><code class="plain">System.out.print(node.data + </code><code class="string">" "</code><code class="plain">);</code></div><div class="line number32 index31 alt1"><code class="undefined spaces">            </code><code class="plain">node = node.next;</code></div><div class="line number33 index32 alt2"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number34 index33 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number35 index34 alt2"> </div><div class="line number36 index35 alt1"><code class="undefined spaces">    </code><code class="plain">Node sortedmerge(Node node1, </code></div><div class="line number37 index36 alt2"><code class="undefined spaces">                     </code><code class="plain">Node node2) </code></div><div class="line number38 index37 alt1"><code class="undefined spaces">    </code><code class="plain">{        </code></div><div class="line number39 index38 alt2"><code class="undefined spaces">        </code><code class="comments">// If both the nodes are null</code></div><div class="line number40 index39 alt1"><code class="undefined spaces">        </code><code class="keyword">if</code> <code class="plain">(node1 == </code><code class="keyword">null</code> <code class="plain">&& </code></div><div class="line number41 index40 alt2"><code class="undefined spaces">            </code><code class="plain">node2 == </code><code class="keyword">null</code><code class="plain">) </code></div><div class="line number42 index41 alt1"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number43 index42 alt2"><code class="undefined spaces">            </code><code class="keyword">return</code> <code class="keyword">null</code><code class="plain">;</code></div><div class="line number44 index43 alt1"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number45 index44 alt2"> </div><div class="line number46 index45 alt1"><code class="undefined spaces">        </code><code class="comments">// Resultant node</code></div><div class="line number47 index46 alt2"><code class="undefined spaces">        </code><code class="plain">Node res = </code><code class="keyword">null</code><code class="plain">;</code></div><div class="line number48 index47 alt1"> </div><div class="line number49 index48 alt2"><code class="undefined spaces">        </code><code class="comments">// If both of them have nodes </code></div><div class="line number50 index49 alt1"><code class="undefined spaces">        </code><code class="comments">// present traverse them</code></div><div class="line number51 index50 alt2"><code class="undefined spaces">        </code><code class="keyword">while</code> <code class="plain">(node1 != </code><code class="keyword">null</code> <code class="plain">&& </code></div><div class="line number52 index51 alt1"><code class="undefined spaces">               </code><code class="plain">node2 != </code><code class="keyword">null</code><code class="plain">) </code></div><div class="line number53 index52 alt2"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number54 index53 alt1"> </div><div class="line number55 index54 alt2"><code class="undefined spaces">            </code><code class="comments">// Now compare both nodes </code></div><div class="line number56 index55 alt1"><code class="undefined spaces">            </code><code class="comments">// current data</code></div><div class="line number57 index56 alt2"><code class="undefined spaces">            </code><code class="keyword">if</code> <code class="plain">(node1.data <= node2.data) </code></div><div class="line number58 index57 alt1"><code class="undefined spaces">            </code><code class="plain">{</code></div><div class="line number59 index58 alt2"><code class="undefined spaces">                </code><code class="plain">Node temp = node1.next;</code></div><div class="line number60 index59 alt1"><code class="undefined spaces">                </code><code class="plain">node1.next = res;</code></div><div class="line number61 index60 alt2"><code class="undefined spaces">                </code><code class="plain">res = node1;</code></div><div class="line number62 index61 alt1"><code class="undefined spaces">                </code><code class="plain">node1 = temp;</code></div><div class="line number63 index62 alt2"><code class="undefined spaces">            </code><code class="plain">} </code></div><div class="line number64 index63 alt1"><code class="undefined spaces">            </code><code class="keyword">else</code></div><div class="line number65 index64 alt2"><code class="undefined spaces">            </code><code class="plain">{</code></div><div class="line number66 index65 alt1"><code class="undefined spaces">                </code><code class="plain">Node temp = node2.next;</code></div><div class="line number67 index66 alt2"><code class="undefined spaces">                </code><code class="plain">node2.next = res;</code></div><div class="line number68 index67 alt1"><code class="undefined spaces">                </code><code class="plain">res = node2;</code></div><div class="line number69 index68 alt2"><code class="undefined spaces">                </code><code class="plain">node2 = temp;</code></div><div class="line number70 index69 alt1"><code class="undefined spaces">            </code><code class="plain">}</code></div><div class="line number71 index70 alt2"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number72 index71 alt1"> </div><div class="line number73 index72 alt2"><code class="undefined spaces">        </code><code class="comments">// If second list reached end, </code></div><div class="line number74 index73 alt1"><code class="undefined spaces">        </code><code class="comments">// but first list has nodes. </code></div><div class="line number75 index74 alt2"><code class="undefined spaces">        </code><code class="comments">// Add remaining nodes of first</code></div><div class="line number76 index75 alt1"><code class="undefined spaces">        </code><code class="comments">// list at the front of result list</code></div><div class="line number77 index76 alt2"><code class="undefined spaces">        </code><code class="keyword">while</code> <code class="plain">(node1 != </code><code class="keyword">null</code><code class="plain">) </code></div><div class="line number78 index77 alt1"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number79 index78 alt2"><code class="undefined spaces">            </code><code class="plain">Node temp = node1.next;</code></div><div class="line number80 index79 alt1"><code class="undefined spaces">            </code><code class="plain">node1.next = res;</code></div><div class="line number81 index80 alt2"><code class="undefined spaces">            </code><code class="plain">res = node1;</code></div><div class="line number82 index81 alt1"><code class="undefined spaces">            </code><code class="plain">node1 = temp;</code></div><div class="line number83 index82 alt2"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number84 index83 alt1"> </div><div class="line number85 index84 alt2"><code class="undefined spaces">        </code><code class="comments">// If first list reached end, </code></div><div class="line number86 index85 alt1"><code class="undefined spaces">        </code><code class="comments">// but second list has node. </code></div><div class="line number87 index86 alt2"><code class="undefined spaces">        </code><code class="comments">// Add remaining nodes of first </code></div><div class="line number88 index87 alt1"><code class="undefined spaces">        </code><code class="comments">// list at the front of result list</code></div><div class="line number89 index88 alt2"><code class="undefined spaces">        </code><code class="keyword">while</code> <code class="plain">(node2 != </code><code class="keyword">null</code><code class="plain">) </code></div><div class="line number90 index89 alt1"><code class="undefined spaces">        </code><code class="plain">{</code></div><div class="line number91 index90 alt2"><code class="undefined spaces">            </code><code class="plain">Node temp = node2.next;</code></div><div class="line number92 index91 alt1"><code class="undefined spaces">            </code><code class="plain">node2.next = res;</code></div><div class="line number93 index92 alt2"><code class="undefined spaces">            </code><code class="plain">res = node2;</code></div><div class="line number94 index93 alt1"><code class="undefined spaces">            </code><code class="plain">node2 = temp;</code></div><div class="line number95 index94 alt2"><code class="undefined spaces">        </code><code class="plain">}</code></div><div class="line number96 index95 alt1"> </div><div class="line number97 index96 alt2"><code class="undefined spaces">        </code><code class="keyword">return</code> <code class="plain">res;</code></div><div class="line number98 index97 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number99 index98 alt2"> </div><div class="line number100 index99 alt1"><code class="undefined spaces">    </code><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args) </code></div><div class="line number101 index100 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number102 index101 alt1"><code class="undefined spaces">        </code><code class="plain">LinkedList list = </code><code class="keyword">new</code> <code class="plain">LinkedList();</code></div><div class="line number103 index102 alt2"><code class="undefined spaces">        </code><code class="plain">Node result = </code><code class="keyword">null</code><code class="plain">;</code></div><div class="line number104 index103 alt1"> </div><div class="line number105 index104 alt2"><code class="undefined spaces">        </code><code class="comments">/* Let us create two sorted linked lists </code></div><div class="line number106 index105 alt1"><code class="undefined spaces">           </code><code class="comments">to test the above functions. Created </code></div><div class="line number107 index106 alt2"><code class="undefined spaces">           </code><code class="comments">lists shall be</code></div><div class="line number108 index107 alt1"><code class="undefined spaces">           </code><code class="comments">a: 5->10->15</code></div><div class="line number109 index108 alt2"><code class="undefined spaces">           </code><code class="comments">b: 2->3->20 */</code></div><div class="line number110 index109 alt1"><code class="undefined spaces">        </code><code class="plain">list.a = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">5</code><code class="plain">);</code></div><div class="line number111 index110 alt2"><code class="undefined spaces">        </code><code class="plain">list.a.next = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">10</code><code class="plain">);</code></div><div class="line number112 index111 alt1"><code class="undefined spaces">        </code><code class="plain">list.a.next.next = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">15</code><code class="plain">);</code></div><div class="line number113 index112 alt2"> </div><div class="line number114 index113 alt1"><code class="undefined spaces">        </code><code class="plain">list.b = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">2</code><code class="plain">);</code></div><div class="line number115 index114 alt2"><code class="undefined spaces">        </code><code class="plain">list.b.next = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">3</code><code class="plain">);</code></div><div class="line number116 index115 alt1"><code class="undefined spaces">        </code><code class="plain">list.b.next.next = </code><code class="keyword">new</code> <code class="plain">Node(</code><code class="value">20</code><code class="plain">);</code></div><div class="line number117 index116 alt2"> </div><div class="line number118 index117 alt1"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">"List a before merge :"</code><code class="plain">);</code></div><div class="line number119 index118 alt2"><code class="undefined spaces">        </code><code class="plain">list.printlist(a);</code></div><div class="line number120 index119 alt1"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">""</code><code class="plain">);</code></div><div class="line number121 index120 alt2"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">"List b before merge :"</code><code class="plain">);</code></div><div class="line number122 index121 alt1"><code class="undefined spaces">        </code><code class="plain">list.printlist(b);</code></div><div class="line number123 index122 alt2"> </div><div class="line number124 index123 alt1"><code class="undefined spaces">        </code><code class="comments">// Merge two sorted linkedlist in </code></div><div class="line number125 index124 alt2"><code class="undefined spaces">        </code><code class="comments">// decreasing order</code></div><div class="line number126 index125 alt1"><code class="undefined spaces">        </code><code class="plain">result = list.sortedmerge(a, b);</code></div><div class="line number127 index126 alt2"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">""</code><code class="plain">);</code></div><div class="line number128 index127 alt1"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">"Merged linked list : "</code><code class="plain">);</code></div><div class="line number129 index128 alt2"><code class="undefined spaces">        </code><code class="plain">list.printlist(result);</code></div><div class="line number130 index129 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number131 index130 alt2"><code class="plain">}</code></div><div class="line number132 index131 alt1"><code class="comments">// This code is contributed by Mayank Jaiswal</code></div></div></td></tr></tbody></table></div>


Output: 

List A before merge: 
5 10 15 
List B before merge: 
2 3 20 
Merged Linked List is: 
20 15 10 5 3 2 

Time Complexity: O(N)

Auxiliary Space: O(1)

This solution traverses both lists only once, doesn’t require reverse and works in-place.

Please refer complete article on Merge two sorted linked lists such that merged list is in reverse order for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
22 Mar, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments