go: add «2181. Merge Nodes in Between Zeros»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
bcabf01c65
commit
a837889497
1 changed files with 18 additions and 0 deletions
18
go/merge-nodes-in-between-zeros.go
Normal file
18
go/merge-nodes-in-between-zeros.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func mergeNodes(head *ListNode) *ListNode {
|
||||||
|
head = head.Next
|
||||||
|
if head == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
node := head.Next
|
||||||
|
for node.Val != 0 {
|
||||||
|
head.Val += node.Val
|
||||||
|
node = node.Next
|
||||||
|
}
|
||||||
|
|
||||||
|
head.Next = mergeNodes(node)
|
||||||
|
|
||||||
|
return head
|
||||||
|
}
|
Loading…
Reference in a new issue