Skip to content

Commit 65f77b6

Browse files
authored
merge two sorted lists solution
1 parent 87da9b6 commit 65f77b6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

merge-two-sorted-lists/yhkee0404.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
9+
ans := &ListNode{}
10+
l1 := list1
11+
l2 := list2
12+
l3 := ans
13+
for l1 != nil || l2 != nil {
14+
if l2 == nil || l1 != nil && l1.Val < l2.Val {
15+
l3.Next = l1
16+
l1 = l1.Next
17+
} else {
18+
l3.Next = l2
19+
l2 = l2.Next
20+
}
21+
l3 = l3.Next
22+
}
23+
return ans.Next
24+
}

0 commit comments

Comments
 (0)