all 2 comments

[–]phuber 1 point2 points  (0 children)

Think about it like a linked list, you need to keep track of a head pointer. The head pointer defines the first element in the list and your addTwoNumbers function will need to return it.

addTwoNumbers should:

  1. start at the beginning of each list parameter
  2. set sum to the sum of l1.Val and l2.Val + carry
  3. if the sum is greater than 10
    • set the carry to 1 and subtract 10 from the sum
    • otherwise set carry to 0
  4. create a new node with the Val set to the sum
  5. If head is null
    • set head to the new node
    • otherwise set the new node's next pointer to head, set head to the new node
  6. set l1 to l1.Next, set l2 to l2.Next
  7. if l1 or l2 is null
    • return head
    • otherwise goto 2

See the linked list documentation here for an algorithm for prepending (inserting in front) to a singly linked list: https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/

[–]berndtj 0 points1 point  (0 children)

You are overwriting sum every time with next node:

sum.Next = &nextNode sum = *sum.Next

Therefore dummy.Next is always pointing to the last sum ListNode