Merge two sorted linked lists

Merge two sorted linked lists

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    static class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;

        public SinglyLinkedListNode(int nodeData) {
            this.data = nodeData;
            this.next = null;
        }
    }

    static class SinglyLinkedList {
        public SinglyLinkedListNode head;
        public SinglyLinkedListNode tail;

        public SinglyLinkedList() {
            this.head = null;
            this.tail = null;
        }

        public void insertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);

            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }

            this.tail = node;
        }
    }

    public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
        while (node != null) {
            bufferedWriter.write(String.valueOf(node.data));

            node = node.next;

            if (node != null) {
                bufferedWriter.write(sep);
            }
        }
    }

    // Complete the mergeLists function below.

    /*
     * For your reference:
     *
     * SinglyLinkedListNode {
     *     int data;
     *     SinglyLinkedListNode next;
     * }
     *
     */
    static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
        SinglyLinkedListNode out;
        if(head1.data <= head2.data){
        out = new SinglyLinkedListNode(head1.data);
        head1 = head1.next;
        }else{
            out = new SinglyLinkedListNode(head2.data);
            head2 = head2.next;
        }
        SinglyLinkedListNode last = out;
        while(head1 != null || head2 != null){
            
            if(head1 == null){
                last.next = head2;
                last = last.next;
                head2 = head2.next;
            }else if( head2 == null){
                last.next = head1;
                last = last.next;
                head1 = head1.next;
            }
            else if(head1.data <= head2.data){
                last.next = head1;
                last = last.next;
                head1 = head1.next;
            }else if(head1.data > head2.data){
                last.next = head2;
                last = last.next;
                head2 = head2.next;
            }
        }

return out;
    }

    private static final Scanner scanner = new Scanner(System.in);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s