Skip to content
Snippets Groups Projects

Merge Two Sorted Lists

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Kiryuu Sakuya
    Program.cs 2.68 KiB
    using System;
    using System.Collections.Generic;
    
    namespace leetcodecsharp
    {
        class Program
        {
            public class ListNode
            {
                public int val;
                public ListNode next;
                public ListNode(int val = 0, ListNode next = null)
                {
                    this.val = val;
                    this.next = next;
                }
            }
            public static ListNode Function(ListNode list1, ListNode list2)
            {
                ListNode c = null;
                ListNode temp = null;
                if (list1 == null && list2 == null)
                    return null;
                if (list1 == null)
                    return list2;
                if (list2 == null)
                    return list1;
                while (list1 != null && list2 != null)
                {
                    if (list1.val < list2.val)
                    {
                        c = insert(c, list1.val);
                        list1 = list1.next;
                    }
                    else
                    {
                        c = insert(c, list2.val);
                        list2 = list2.next;
                    }
                }
                if (list1 == null)
                {
                    temp = c;
                    while (temp.next != null)
                    {
                        temp = temp.next;
                    }
                    temp.next = list2;
                }
                    
                if (list2 == null)
                {
                    temp = c;
                    while (temp.next != null)
                    {
                        temp = temp.next;
                    }
                    temp.next = list1;
                }
                return c;
            }
    
            public static ListNode insert(ListNode root, int item)
            {
                ListNode temp = new ListNode();
                ListNode ptr;
                temp.val = item;
                temp.next = null;
    
                if (root == null)
                    root = temp;
                else
                {
                    ptr = root;
                    while (ptr.next != null)
                        ptr = ptr.next;
                    ptr.next = temp;
                }
                return root;
            }
            
            public static ListNode ArrayToList(int []arr, int n)
            {
                ListNode root = null;
                for (int i = 0; i < n; i++)
                {
                    root = insert(root, arr[i]);
                }
                return root;
            }
    
    
            public static void Main(string[] args)
            {
                int []list1 = { 5 };
                int []list2 = { 1, 2, 4 };
                int n1 = 1;
                int n2 = 3;
                ListNode a = ArrayToList(list1, n1);
                ListNode b = ArrayToList(list2, n2);
                //ListNode b = null;
                Console.WriteLine(Function(a, b));
            }
    
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment