golang code gives correct output when run locally
Anonymous User
169

This is my first attempt at leetcode, I wrote this code testing locally and its gives me correct output for all the use cases that I've provided to it. But when I run this code on leetcode it fails every time.
I've posted the main function as well that I am using to test locally.
I've run through my logic in my head and on paper. If the structure ListNode is the way provided, then my code should definitely work.
Inputs I've tested against:
[2,4,3]
[5,6,4]
[0]
[0]
[9,9,9,9,9,9,9]
[9,9,9,9]

Outputs for the above inputs:
[7,0,1]
[0]
[8,9,9,0,0,0,1]

The expected and the locally produced results:
[7,0,8]
[0]
[8,9,9,9,0,0,0,1]

I have no idea why it wouldn't work here. Is this also part of starting off at leetcode?
Any help would be appreciated.
Thanks.

package main

import (
	"fmt"
)


// Definition for singly-linked list.
type ListNode struct {
	Val int
	Next *ListNode
}

func shifter(node *ListNode) *ListNode {
	newPointer := node
	newPointer.Next = &ListNode{}
	newPointer.Next.Val = newPointer.Val / 10
	newPointer.Val = newPointer.Val % 10

	return newPointer
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	var head3 *ListNode = &ListNode{}
	var list3Counter *ListNode = head3
	var list2Counter *ListNode = l2
	for list1Counter := l1; list1Counter.Next != nil; list1Counter = list1Counter.Next {
		if list2Counter.Next != nil {
			list3Counter.Val += list1Counter.Val + list2Counter.Val
			if list3Counter.Val >= 10 {
				list3Counter = shifter(list3Counter)
				// fmt.Println("overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
			} else {
				list3Counter.Next = &ListNode{}
				// fmt.Println(list1Counter.Next, ", no overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
			}
			list2Counter = list2Counter.Next
		} else {
			list3Counter.Val += list1Counter.Val
			if list3Counter.Val >= 10 {
				// fmt.Println("only list1, overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
				list3Counter = shifter(list3Counter)
			} else {
				list3Counter.Next = &ListNode{}
				// fmt.Println("only list1, no overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
			}
		}
		list3Counter = list3Counter.Next
	}

	for ; list2Counter.Next != nil; list2Counter = list2Counter.Next {
		list3Counter.Val += list2Counter.Val
		if list3Counter.Val >= 10 {
			list3Counter = shifter(list3Counter)
			// fmt.Println("only list2, overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
		} else {
			list3Counter.Next = &ListNode{}
			// fmt.Println("only list2, no overflow happened, new node created: ", list3Counter.Val, " : ", list3Counter.Next)
		}
		list3Counter = list3Counter.Next
	}
	return head3
}
func main() {
	var head1 *ListNode = &ListNode{}
	var l1 *ListNode = head1
	for items1 := 0; items1 < 4; items1++ {
		l1.Val = items1 + 5
		l1.Next = &ListNode{}
		l1 = l1.Next

	}

	l1 = head1
	l1.Val = 9
	l1.Next.Val = 9
	l1.Next.Next.Val = 9
	l1.Next.Next.Next.Val = 9


	fmt.Println("List 1")

	for counter1 := head1; counter1.Next != nil; counter1 = counter1.Next {
		fmt.Println(counter1.Val, counter1.Next)
	}


	var head2 *ListNode = &ListNode{}
	var l2 *ListNode = head2
	for items2 := 0; items2 < 7; items2++ {
		l2.Val = items2
		l2.Next = &ListNode{}
		l2 = l2.Next

	}

	l2 = head2
	l2.Val = 9
	l2.Next.Val = 9
	l2.Next.Next.Val = 9
	l2.Next.Next.Next.Val = 9
	l2.Next.Next.Next.Next.Val = 9
	l2.Next.Next.Next.Next.Next.Val = 9
	l2.Next.Next.Next.Next.Next.Next.Val = 9

	fmt.Println("List 2")

	for counter2 := head2; counter2.Next != nil; counter2 = counter2.Next {
		fmt.Println(counter2.Val, counter2.Next)
	}

	var arr *ListNode = addTwoNumbers(head1, head2)
	for counter := arr; counter.Next != nil; counter = counter.Next {
		fmt.Println(counter.Val)
	}

}
Comments (0)