Home

C# Linked Lists And Merging

NOTE!! While this can help process and understand what linked lists are and do - C# already has a MUCH simpler to use LinkedList class - https://www.geeksforgeeks.org/linked-list-implementation-in-c-sharp/

public class Solution
{
    //https://leetcode.com/problems/merge-two-sorted-lists/

    static void Main()
    {
        while (true)
        {
            Console.WriteLine("Enter the first listNode");
            string s = Console.ReadLine();
            ListNode list1 = BuildListNodeFromString(s);

            Console.WriteLine("Enter the second listNode");
            s = Console.ReadLine();
            ListNode list2 = BuildListNodeFromString(s);

            Console.WriteLine();
            Console.WriteLine("The output is - ");
            ListNode showMe =  MergeTwoLists(list1, list2);

            while(showMe != null)
            {
                Console.WriteLine(showMe.val);
                showMe = showMe.next;
            }
        }
    }

    static ListNode BuildListNodeFromString(string s)
    {
        s = s.Replace("[", "");
        s = s.Replace("]", "");
        string[] sArray = s.Split(",");
        int[] iArray = Array.ConvertAll(sArray, x => int.Parse(x));

        ListNode[] listNodeArray = new ListNode[iArray.Length];
        for (int i = 0; i < iArray.Length; i++)
        {
            listNodeArray[i] = new ListNode(iArray[i], null);
        }
        for (int i = 0; i < listNodeArray.Length - 1; i++)
        {
            listNodeArray[i].next = listNodeArray[i + 1];
        }

        return listNodeArray[0];
    }

    public static ListNode MergeTwoLists(ListNode list1, ListNode list2)
    {
        List<int> listInt = new List<int>();

        while (list1 != null && list2 != null)
        {
            if (list1.val <= list2.val)
            {
                listInt.Add(list1.val);
                list1 = list1.next;
            }
            else
            {
                listInt.Add(list2.val);
                list2 = list2.next;
            }
        }
        while (list1 != null)
        {
            if (list1 != null)
            {
                listInt.Add(list1.val);
                list1 = list1.next;
            }
        }
        while (list2 != null)
        {
            if (list2 != null)
            {
                listInt.Add(list2.val);
                list2 = list2.next;
            }
        }

        ListNode[] listNodeArray = new ListNode[listInt.Count];
        for (int i = 0; i < listInt.Count; i++)
        {
            listNodeArray[i] = new ListNode(listInt[i], null);
        }
        for (int i = 0; i < listNodeArray.Length - 1; i++)
        {
            listNodeArray[i].next = listNodeArray[i + 1];
        }

        if (listNodeArray.Length == 0)
        {
            return null;
        }
        else
        {
            return listNodeArray[0];
        }
    }

    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int val = 0, ListNode next = null)
        {
            this.val = val;
            this.next = next;
        }
    }
}

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog