Home > Enterprise >  Adding each element from a linked list
Adding each element from a linked list

Time:07-02

I am trying to sum 2 linked lists, element by element, the result will be put into a new list.

Example :

Input : list1 : 5->3->4

        list2 : 6->1->2

Output : list3 : 11->4->6

Here's what i've done so far :

internal class Program
    {
        static void Main(string[] args)
        {
           ListNode l1 = new ListNode(1);
            l1.next = new ListNode(2);
            l1.next.next = new ListNode(3);

            ListNode l2 = new ListNode(1);
            l2.next = new ListNode(2);
            l2.next.next = new ListNode(3);

            ListNode l3 = new ListNode();
            

            while (l1 != null && l2 != null)
            {

                l3.val  = l1.val   l2.val;
                l1 = l1.next;
                l2 = l2.next;
                l3.next = new ListNode();
            }
            
            while(l3 != null)
            {
                Console.WriteLine(l3.val   " ->");
                l3 = l3.next;   

            }
        }

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

    }

My code sums up the lists, but not for each element, for example the output for the above code is list3 : 12->0. Any ideea why is this happening?

CodePudding user response:

I see what you want to do here and I think the main reason, why this do not work, are how you work with your lists.

But first of all, I want to encourage you, to use the default List from c#, which implements all you need, here you have a solution with list's from c#:

static void Main(string[] args)
        {
            List<int> list1 = new List<int>();
           //adding the values to list1
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);

            List<int> list2 = new List<int>();
            //adding the values to list2
            list2.Add(1);
            list2.Add(2);
            list2.Add(3);

            List<int> list3 = new List<int>();

            //The list lenght's are similar to each other, which is important
            if(list1.Count == list2.Count)
            {
                //loop to add the values together
                for(int i = 0; i < list1.Count; i  )
                {
                    list3.Add(list1[i]   list2[i]);
                }
            }
            
            foreach(int i in list3)
            {
                Console.Write(i   " ->");
            }
            //Output: 2 ->4 ->6 ->
        }

To explain this piece of code: list1 and list2 are both a List, which implement every function you need, like .Add(), .Insert() etc... and I add the values 1,2 and 3 to both list and sum them together and add every sum into list3.

But I guess, you want to build your own list, because it makes you really understand, how these datastucture works!

If it is that, what you want, then I can Edit my answer for you, with an easy implementation of a list.

CodePudding user response:

You're not setting l3 to the next node. you need to do this:

while (l1 != null && l2 != null)
{

    l3.val  = l1.val   l2.val;
    l1 = l1.next;
    l2 = l2.next;
    l3.next = new ListNode();
    l3 = l3.next; //Add this line
}

CodePudding user response:

There are multiple issues with your code:

  1. You are not changing the current node when iterating for sum
  2. You are adding extra empty node

You can try next approach - create temporary current node which will be changed and updated in the loop:

ListNode l3 = new ListNode();
ListNode curr = null;
while (l1 != null && l2 != null)
{
    if(curr == null)
    {
        curr = l3;
    }
    else
    {
        curr.next = new ListNode();
        curr = curr.next;
    }

    curr.val  = l1.val   l2.val;
    l1 = l1.next;
    l2 = l2.next;
}

Also you need to change output code. For example:

Console.Write(l3.val); 
while (l3.next != null)
{
    l3 = l3.next;
    Console.Write(" -> "   l3.val);
}

Both pieces of code assume that both incoming lists have at least one element.

  • Related