Merge two sorted linked list.

Merge Linked list Problem 3 

Merge Snippet whithout using recursion

static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2)
    {
        SinglyLinkedListNode head= null;
        if(head1==null)
        {
            return head2;
        }
        if(head2==null)
        {
            return head1;
        }
        
        if(head1.data< =head2.data)
        {
            head= head1;
            head1=head1.next;
        }
        else
        {
            head= head2;
            head2=head2.next;
        } 
                
        SinglyLinkedListNode temp= head;
        while(head1!=null && head2!=null)
        {
            if(head1.data< =head2.data)
            {
                temp.next=head1;
                temp=temp.next;
                head1=head1.next;   
            }
            else
            {
                temp.next=head2;
                temp=temp.next;
                head2=head2.next;
            } 
        }
        if(head1!=null)
        {
            temp.next= head1;
        }
        else
        {
            temp.next=head2;
        }
        return head;
      
    }
    

Full Code Snippet

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;

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

    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;
        }
    }

    static void PrintSinglyLinkedList(SinglyLinkedListNode node, string sep, TextWriter textWriter) {
        while (node != null) {
            textWriter.Write(node.data);

            node = node.next;

            if (node != null) {
                textWriter.Write(sep);
            }
        }
    }

    static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2)
    {
        SinglyLinkedListNode head= null;
        if(head1==null)
        {
            return head2;
        }
        if(head2==null)
        {
            return head1;
        }
        
        if(head1.data< = head2.data)
        {
            head= head1;
            head1=head1.next;
        }
        else
        {
            head= head2;
            head2=head2.next;
        } 
                
        SinglyLinkedListNode temp= head;
        while(head1!=null && head2!=null)
        {
            if(head1.data< = head2.data)
            {
                temp.next=head1;
                temp=temp.next;
                head1=head1.next;   
            }
            else
            {
                temp.next=head2;
                temp=temp.next;
                head2=head2.next;
            } 
        }
        if(head1!=null)
        {
            temp.next= head1;
        }
        else
        {
            temp.next=head2;
        }
        return head;
      
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int tests = Convert.ToInt32(Console.ReadLine());

        for (int testsItr = 0; testsItr < tests; testsItr++) {
            SinglyLinkedList llist1 = new SinglyLinkedList();

            int llist1Count = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < llist1Count; i++) {
                int llist1Item = Convert.ToInt32(Console.ReadLine());
                llist1.InsertNode(llist1Item);
            }
          
          	SinglyLinkedList llist2 = new SinglyLinkedList();

            int llist2Count = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < llist2Count; i++) {
                int llist2Item = Convert.ToInt32(Console.ReadLine());
                llist2.InsertNode(llist2Item);
            }

            SinglyLinkedListNode llist3 = mergeLists(llist1.head, llist2.head);

            PrintSinglyLinkedList(llist3, " ", textWriter);
            textWriter.WriteLine();
        }

        textWriter.Flush();
        textWriter.Close();
    }
}

Comments

Popular Posts