Add Two Numbers II Solutions in Go
Number 445
Difficulty Medium
Acceptance 54.6%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("github.com/halfrost/LeetCode-Go/structures")// ListNode definetype ListNode = structures.ListNode/*** Definition for singly-linked list.* type ListNode struct {* Val int* Next *ListNode* }*/func addTwoNumbers445(l1 *ListNode, l2 *ListNode) *ListNode {if l1 == nil {return l2}if l2 == nil {return l1}l1Length := getLength(l1)l2Length := getLength(l2)newHeader := &ListNode{Val: 1, Next: nil}if l1Length < l2Length {newHeader.Next = addNode(l2, l1, l2Length-l1Length)} else {newHeader.Next = addNode(l1, l2, l1Length-l2Length)}if newHeader.Next.Val > 9 {newHeader.Next.Val = newHeader.Next.Val % 10return newHeader}return newHeader.Next}func addNode(l1 *ListNode, l2 *ListNode, offset int) *ListNode {if l1 == nil {return nil}var (res, node *ListNode)if offset == 0 {res = &ListNode{Val: l1.Val + l2.Val, Next: nil}node = addNode(l1.Next, l2.Next, 0)} else {res = &ListNode{Val: l1.Val, Next: nil}node = addNode(l1.Next, l2, offset-1)}if node != nil && node.Val > 9 {res.Val++node.Val = node.Val % 10}res.Next = nodereturn res}func getLength(l *ListNode) int {count := 0cur := lfor cur != nil {count++cur = cur.Next}return count}