Merge two sorted linked lists such that merged list is in reverse order

Merge two sorted linked lists such that merged list is in reverse order

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

Same as {@link SortedListIntersection}

Basic Logic:

  • If any of list is empty then return non-empty list
  • Iterate till both lists nodes are not empty.
  • If node1.data > node2.data then move of step a head in list2
  • If node1.data < node2.data then move of step a head in list1
  • If node1.data == node2.data, then add data to result list and increase both list pointers
  • When Adding to result list add it to front of list

Latest Source Code:
Github: ReverseMerge.java


Output:

List1: 5 10 15 40 
List2: 2 3 20 
Reversed Merged List: 40 20 15 10 5 3 2 
Author: Hrishikesh Mishra