-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b001382
commit aeec823
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 2. Add Two Numbers | ||
|
||
[Add Two Numbers - LeetCode](https://leetcode.com/problems/add-two-numbers/description/) | ||
|
||
## description | ||
|
||
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 contain a single digit. Add the two numbers and return it as a linked list. | ||
|
||
You may assume the two numbers do not contain any leading zero, except the number 0 itself. | ||
|
||
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) | ||
Output: 7 -> 0 -> 8 | ||
|
||
## Summary | ||
|
||
## tips |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package addTwoNumbers | ||
|
||
// ListNode Definition for singly-linked list. | ||
type ListNode struct { | ||
Val int | ||
Next *ListNode | ||
} | ||
|
||
// Compare ListNode | ||
func Compare(l1 *ListNode, l2 *ListNode) bool { | ||
c1, c2 := scan(l1), scan(l2) | ||
for { | ||
// ok is false if there are no more values to receive and the channel is closed. | ||
v1, ok1 := <-c1 | ||
v2, ok2 := <-c2 | ||
if !ok1 || !ok2 { | ||
return ok1 == ok2 | ||
} | ||
if v1 != v2 { | ||
break | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { | ||
dummyHead := &ListNode{Val: 0} | ||
p := l1 | ||
q := l2 | ||
curr := dummyHead | ||
carry := 0 | ||
for p != nil || q != nil { | ||
var x int | ||
if p != nil { | ||
x = p.Val | ||
} else { | ||
x = 0 | ||
} | ||
var y int | ||
if q != nil { | ||
y = q.Val | ||
} else { | ||
y = 0 | ||
} | ||
sum := carry + x + y | ||
carry = sum / 10 | ||
curr.Next = &ListNode{Val: sum % 10} | ||
curr = curr.Next | ||
if p != nil { | ||
p = p.Next | ||
} | ||
if q != nil { | ||
q = q.Next | ||
} | ||
} | ||
if carry > 0 { | ||
curr.Next = &ListNode{Val: carry} | ||
} | ||
return dummyHead.Next | ||
} | ||
|
||
// func addTwoNumbersParallel(l1 *ListNode, l2 *ListNode) *ListNode { | ||
// c1, c2 := scan(l1), scan(l2) | ||
// var ns []int | ||
// for { | ||
// // ok is false if there are no more values to receive and the channel is closed. | ||
// v1, ok1 := <-c1 | ||
// v2, ok2 := <-c2 | ||
// if ok1 && ok2 { | ||
// ns = append(ns, v1+v2) | ||
// } | ||
// if !ok1 && !ok2 { | ||
// break | ||
// } | ||
// } | ||
// size := len(ns) | ||
|
||
// for index, digit := range ns { | ||
// if digit >= 10 { | ||
// if index+1 == size { | ||
// ns[index+1] = 1 | ||
// } else { | ||
// ns[index+1] = ns[index+1] + 1 | ||
// } | ||
// ns[index] = ns[index] - 10 | ||
// } | ||
// } | ||
|
||
// headNode := &ListNode{Val: ns[0]} | ||
// ch := getLinkNode(ns[1:]) | ||
// go link(headNode, ch) | ||
|
||
// return headNode | ||
// } | ||
|
||
// func getLinkNode(ns []int) chan *ListNode { | ||
// c := make(chan *ListNode) | ||
// go func() { | ||
// var wg sync.WaitGroup | ||
// wg.Add(len(ns)) | ||
// for _, v := range ns { | ||
// go func(v int) { | ||
// defer wg.Done() | ||
// c <- &ListNode{Val: v} | ||
// }(v) | ||
// } | ||
// wg.Wait() | ||
// close(c) | ||
// }() | ||
// return c | ||
// } | ||
|
||
func scanListNode(l *ListNode, ch chan int) { | ||
if l == nil { | ||
return | ||
} | ||
ch <- l.Val | ||
if l.Next != nil { | ||
scanListNode(l.Next, ch) | ||
} | ||
} | ||
|
||
// Walker launches Walk in a new goroutine, | ||
// and returns a read-only channel of values. | ||
func scan(l *ListNode) <-chan int { | ||
ch := make(chan int) | ||
go func() { | ||
scanListNode(l, ch) | ||
close(ch) | ||
}() | ||
return ch | ||
} | ||
|
||
// func link(l *ListNode, ch chan *ListNode) { | ||
// lnNext, ok := <-ch | ||
// if ok { | ||
// l.Next = lnNext | ||
// link(lnNext, ch) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package addTwoNumbers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAddTwoNumbers(t *testing.T) { | ||
input1 := &ListNode{ | ||
Val: 2, | ||
Next: &ListNode{ | ||
Val: 4, | ||
Next: &ListNode{ | ||
Val: 3, | ||
Next: nil, | ||
}, | ||
}, | ||
} | ||
input2 := &ListNode{ | ||
Val: 5, | ||
Next: &ListNode{ | ||
Val: 6, | ||
Next: &ListNode{ | ||
Val: 4, | ||
Next: nil, | ||
}, | ||
}, | ||
} | ||
expected := &ListNode{ | ||
Val: 7, | ||
Next: &ListNode{ | ||
Val: 0, | ||
Next: &ListNode{ | ||
Val: 8, | ||
Next: nil, | ||
}, | ||
}, | ||
} | ||
var cases = []struct { | ||
input1 *ListNode | ||
input2 *ListNode | ||
expected *ListNode | ||
}{ | ||
{input1, input2, expected}, | ||
} | ||
|
||
for _, tt := range cases { | ||
result := addTwoNumbers(tt.input1, tt.input2) | ||
assert.True(t, Compare(result, tt.expected)) | ||
} | ||
} |