Merge two sorted linked list and print in reverse order

Problem: For two given sorted linked list, obtain one merged linked list and display in reverse order

Problem Explanation: For two given linked list 10 -> 20 -> 30 and 26 -> 32 -> 34,  10 -> 20 -> 26 -> 30 -> 32 -> 34 is the merged linked list.

Hence, output is 34 -> 32 -> 30 -> 26 -> 20 -> 10. Similarly, for linked lists 5 -> 20 and 10 -> 15 -> 25, 5 -> 10 -> 15 -> 20 -> 25 is the merged linked list.

Hence, 25 -> 20 -> 15 -> 10 -> 5 is the output.

Java Implementation

public class MergeSortedLL {
    public Node mergeReverseLL(Node first, Node second) {
        Node resultant = null, prev = null;
        if (first == null && second == null) {
            return null;
        }

        while (first != null && second != null) {
            prev = resultant;
            if (first.getValue() < second.getValue()) {
                resultant = new Node(first.getValue(), prev);
                first = first.getLink();
            } else {
                resultant = new Node(second.getValue(), prev);
                second = second.getLink();
            }
        }

        while (first != null) {
            prev = resultant;
            resultant = new Node(first.getValue(), prev);
            first = first.getLink();
        }

        while (second != null) {
            prev = resultant;
            resultant = new Node(second.getValue(), prev);
            second = second.getLink();
        }

        return resultant;
    }

    public void printLL(Node node) {
        while (node != null) {
            System.out.println(node.getValue());
            node = node.getLink();
        }
    }

    public static void main(String[] args) {
        Node first = new Node(10, null);
        first.setLink(new Node(20, null));
        first.getLink().setLink(new Node(30, null));

        Node second = new Node(26, null);
        second.setLink(new Node(32, null));
        second.getLink().setLink(new Node(34, null));

        MergeSortedLL mergeSortedLL = new MergeSortedLL();
        mergeSortedLL.printLL(mergeSortedLL.mergeReverseLL(first, second));

    }

public class Node {
    private int value;
    private Node link;

    public Node(int value, Node n) {
        this.value = value;
        this.link = n;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getLink() {
        return link;
    }

    public void setLink(Node link) {
        this.link = link;
    }
}

TimeComplexity: O(n)

Note: If you find any other better way to approach to this problem or you find any issue/error in above code snippets/approaches – please share it in the comments section below and get a chance to enrol free of cost for an online Live Data Structure and Algorithm course (specially designed for interview preparation for software companies like Amazon, Google, Facebook, FlipKart, SnapDeal, HealthKart…)