You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Constraints:
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
result := &ListNode{}
head := result
round := 0
for l1 != nil && l2 != nil {
if l1.Val+l2.Val+round >= 10 {
sum := l1.Val + l2.Val - 10 + round
round = 1
result.Next = &ListNode{Val: sum}
} else {
sum := l1.Val + l2.Val + round
round = 0
result.Next = &ListNode{Val: sum}
}
result = result.Next
l1 = l1.Next
l2 = l2.Next
}
for l1 != nil {
sum := l1.Val + round
round = 0
if sum >= 10 {
sum = sum - 10
round = 1
}
result.Next = &ListNode{Val: sum}
result = result.Next
l1 = l1.Next
}
for l2 != nil {
sum := l2.Val + round
round = 0
if sum >= 10 {
sum = sum - 10
round = 1
}
result.Next = &ListNode{Val: sum}
result = result.Next
l2 = l2.Next
}
if result.Val >= 10 {
result.Val -= 10
round = 1
}
if round == 1 {
result.Next = &ListNode{Val: 1}
}
return head.Next
}
https://go.dev/play/p/eULtscamIKQ
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var rHead, r *ListNode // result list, its iteration pointer
var overflow int // addition overflow
for l1 != nil || l2 != nil || overflow != 0 {
// extract values from the lists and move their pointers forward
v1, v2 := 0, 0
if l1 != nil {
v1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
v2 = l2.Val
l2 = l2.Next
}
value := v1 + v2 + overflow
if value > 9 {
overflow = 1
value = value % 10
} else {
overflow = 0
}
// append the value to the end of the result list
if rHead == nil {
rHead = &ListNode{value, nil}
r = rHead
} else {
r.Next = &ListNode{value, nil}
r = r.Next
}
}
return rHead
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head := &ListNode{Val: 0}
first, second, carry, current := 0, 0, 0, head
for l1 != nil || l2 != nil || carry != 0 {
first = 0
if l1 != nil {
first = l1.Val
l1 = l1.Next
}
second = 0
if l2 != nil {
second = l2.Val
l2 = l2.Next
}
current.Next = &ListNode{Val: (first + second + carry) % 10}
current = current.Next
carry = (first + second + carry) / 10
}
return head.Next
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
fHead := &ListNode{}
for tail, carry, open := fHead, 0, 2; open + carry > 0; l1, l2 = l1.Next, l2.Next {
tail.Next = &ListNode{}
tail = tail.Next
tail.Val = l1.Val + l2.Val + carry
carry = tail.Val / 10
tail.Val = tail.Val % 10
if l1.Next == nil {
*l1 = ListNode{0, l1}
open--
}
if l2.Next == nil {
*l2 = ListNode{0, l2}
open--
}
}
return fHead.Next
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head := new(ListNode)
cur := head
carry := 0
for l1 != nil || l2 != nil || carry != 0 {
v1, v2 := 0, 0
if l1 != nil {
v1, l1 = l1.Val, l1.Next
}
if l2 != nil {
v2, l2 = l2.Val, l2.Next
}
num := v1 + v2 + carry
carry = num / 10
cur.Next = &ListNode{Val: num%10, Next: nil}
cur = cur.Next
}
return head.Next
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
start := &ListNode {
Val: 0,
}
current := start
previous := start
for true {
if(l1 != nil){
if(l2 != nil){
current.Val = l1.Val + l2.Val + current.Val
l1 = l1.Next
l2 = l2.Next
}else{
current.Val = l1.Val + current.Val
l1 = l1.Next
}
} else if(l2 != nil){
current.Val = l2.Val + current.Val
l1 = l2.Next
l2 = nil
} else{
break
}
if current.Val >= 10 {
current.Next = &ListNode {
Val: 1,
}
current.Val = current.Val % 10
} else {
current.Next = &ListNode {
Val: 0,
}
}
previous = current
current = current.Next
}
if current.Val == 0 {
previous.Next = nil
}
// for l1.Next != nil && l2.Next != nil {
// current.Val = l1.Val + l2.Val + current.Val
// if current.Val >= 10 {
// current.Next = &ListNode {
// Val: 1,
// }
// current.Val = current.Val % 10
// } else {
// current.Next = &ListNode {
// Val: 0,
// }
// }
// current = current.Next
// l1 = l1.Next
// l2 = l2.Next
// }
// current.Val = l1.Val + l2.Val + current.Val
// if current.Val >= 10 {
// current.Next = &ListNode {
// Val: 1,
// }
// current.Val = current.Val % 10
// }
// if l1.Next == nil && l2.Next == nil {
// return start;
// }
// if current.Next == nil {
// current.Next = &ListNode {
// Val: 0,
// }
// }
// current = current.Next
// var n *ListNode
// if l1.Next != nil {
// n = l1.Next
// } else if l2.Next != nil {
// n = l2.Next
// }
// for n.Next != nil {
// current.Val = n.Val + current.Val
// if current.Val >= 10 {
// current.Next = &ListNode {
// Val: 1,
// }
// current.Val = current.Val % 10
// } else {
// current.Next = &ListNode {
// Val: 0,
// }
// }
// current = current.Next
// n = n.Next
// }
// current.Val = n.Val + current.Val
// if current.Val >= 10 {
// current.Next = &ListNode {
// Val: 1,
// }
// current.Val = current.Val % 10
// }
return start;
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
pres := new(ListNode)
pstart := pres
carry := false
for {
if l1 != nil {
pres.Val += l1.Val
l1 = l1.Next
}
if l2 != nil {
pres.Val += l2.Val
l2 = l2.Next
}
if carry {
pres.Val++
}
carry = false
if pres.Val > 9 {
carry = true
}
pres.Val %= 10
if l1 != nil || l2 != nil || carry == true {
pres.Next = new(ListNode)
pres = pres.Next
} else {
return pstart
}
}
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
sum := &ListNode{}
currentNode := sum
carryOver := 0
l1Node := l1
l2Node := l2
for {
sumVal := l1Node.Val + l2Node.Val + carryOver
carryOver = 0
if sumVal >= 10 {
carryOver = 1
sumVal -= 10
}
currentNode.Val = sumVal
if l1Node.Next == nil && l2Node.Next == nil {
if carryOver > 0 {
currentNode.Next = &ListNode{Val: carryOver}
}
break
}
if l1Node.Next != nil {
l1Node = l1Node.Next
} else {
l1Node = &ListNode{}
}
if l2Node.Next != nil {
l2Node = l2Node.Next
} else {
l2Node = &ListNode{}
}
currentNode.Next = &ListNode{}
currentNode = currentNode.Next
}
return sum
}
// var (
// total float64 = 0
// i float64 = 0
// )
// total = sumNode(total, l1)
// total = sumNode(total, l2)
// output := &ListNode{}
// current := output
// i = 1
// for {
// fmt.Printf("%d\n", total)
// currentPower := math.Pow(10, i)
// v := math.Mod(total, currentPower)
// fmt.Println(v)
// total -= v
// divisor := (math.Pow(10, i)/10)
// current.Val = int(v/divisor)
// if total == 0 {
// break
// }
// current.Next = &ListNode{}
// current = current.Next
// i++
// }
// return output
// }
// func sumNode(total float64, node *ListNode) float64 {
// var i float64 = 1
// for {
// val := node.Val
// if val == 0 {
// val = 1
// }
// total += float64(val) * math.Pow(10, i)
// if node.Next == nil {
// break
// }
// node = node.Next
// i++
// }
// return total
// }
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var head, tail *ListNode
carry := 0
for l1 != nil || l2 != nil {
n1, n2 := 0, 0
if l1 != nil {
n1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
n2 = l2.Val
l2 = l2.Next
}
sum := n1 + n2 + carry
sum, carry = sum % 10, sum / 10
if head == nil {
head = &ListNode{Val: sum}
tail = head
} else {
tail.Next = &ListNode{Val: sum}
tail = tail.Next
}
}
if carry > 0 {
tail.Next = &ListNode{Val: carry}
}
return head
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head := &ListNode{}
ln := head
carry := 0
for l1 != nil || l2 != nil || carry > 0 {
v1 , v2 := 0 ,0
if l1 != nil {
v1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
v2 = l2.Val
l2 = l2.Next
}
sum := v1 + v2 + carry
carry = sum / 10
ln.Next = &ListNode{Val : sum % 10 , Next : nil}
ln = ln.Next
}
return head.Next
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head:=l1
flag:=0
for {
l1.Val,flag = (l1.Val+l2.Val+flag)%10,(l1.Val+l2.Val+flag)/10
if l1.Next==nil && l2.Next==nil && flag==0{
break
}
if l1.Next==nil {
l1.Next = &ListNode{0,nil}
}
if l2.Next==nil {
l2.Next = &ListNode{0,nil}
}
l1=l1.Next
l2=l2.Next
}
return head
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := ListNode{}
carry, sum := 0, 0
p, p1, p2 := &dummy, l1, l2
for p1 != nil || p2 != nil || carry != 0 {
sum = carry
if p1 != nil {
sum = sum + p1.Val
p1 = p1.Next
}
if p2 != nil {
sum = sum + p2.Val
p2 = p2.Next
}
carry = sum / 10
p.Next = &ListNode{Val: sum % 10, Next: nil}
p = p.Next
}
return dummy.Next
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var currentResponse *ListNode
var currentL1 *ListNode
var currentL2 *ListNode
var firstResponse = &ListNode{Val: 0, Next: nil}
currentResponse = firstResponse
currentL1 = l1
currentL2 = l2
var hasRemainder bool
for ok := true; ok; ok = (currentL1 != nil && currentL1.Next != nil) || (currentL2 != nil && currentL2.Next != nil)|| hasRemainder {
if currentResponse.Next != nil {
currentResponse = currentResponse.Next
if currentL1 != nil {
currentL1 = currentL1.Next
}
if currentL2 != nil {
currentL2 = currentL2.Next
}
}
var currentL1Val int
var currentL2Val int
if currentL1 != nil {
currentL1Val = currentL1.Val
}
if currentL2 != nil {
currentL2Val = currentL2.Val
}
newValue := currentL1Val + currentL2Val
if hasRemainder {
newValue += 1
}
hasRemainder = false
if newValue >= 10 {
hasRemainder = true
}
currentResponse.Val = newValue % 10
if (currentL1 != nil && currentL1.Next != nil) || (currentL2 != nil && currentL2.Next != nil) || hasRemainder {
currentResponse.Next = &ListNode{Val: 0, Next: nil}
}
}
return firstResponse
}
[Go] Linked list - Merge Two Sorted Lists (0) | 2022.05.14 |
---|---|
[Go] Linked list - Doubly Linked List from java to GO (0) | 2022.05.09 |
[Go] Linked List - Palindrome Linked List (0) | 2022.05.09 |
[Go] Linked List - Odd Even Linked List (0) | 2022.05.06 |
[Go] Linked List - Remove Linked List Elements (0) | 2022.05.05 |
댓글 영역