0002.add-two-numbers

command
v0.0.0-...-3f7d18b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2024 License: MIT Imports: 1 Imported by: 0

README

两数相加

题目链接

题目

给定两个链表,按照正常的加法法则返回他们相加后的结果。

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807

答案


func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	l3 := new(ListNode)
	node3 := l3
	a := 0
	for (l1 != nil) || (l2 != nil) || a > 0 {
		node3.Next = new(ListNode)
		node3 = node3.Next
		b := 0
		c := 0
		if l1 != nil {
			b = l1.Val
		}
		if l2 != nil {
			c = l2.Val
		}
		node3.Val = (a + b + c) % 10
		a = (a + b + c) / 10
		if l1 != nil {
			l1 = l1.Next
		}
		if l2 != nil {
			l2 = l2.Next
		}
	}
	return l3.Next
}

对于数据结构的处理还是有一些不清除的地方,需要继续学习。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL