feat: friday
This commit is contained in:
33
pkg/merge_two_sorted_lists/main.go
Normal file
33
pkg/merge_two_sorted_lists/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package merge_two_sorted_lists
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func MergeTwoLists(a *ListNode, b *ListNode) *ListNode {
|
||||
result := &ListNode{}
|
||||
head := result
|
||||
|
||||
for {
|
||||
if a == nil {
|
||||
if b == nil {
|
||||
break
|
||||
} else {
|
||||
head.Next, head, b = b, b, b.Next
|
||||
}
|
||||
} else {
|
||||
if b == nil {
|
||||
head.Next, head, a = a, a, a.Next
|
||||
} else {
|
||||
if a.Val < b.Val {
|
||||
head.Next, head, a = a, a, a.Next
|
||||
} else {
|
||||
head.Next, head, b = b, b, b.Next
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.Next
|
||||
}
|
||||
Reference in New Issue
Block a user