File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ package hello
2+
3+ type ListNode struct {
4+ Val int
5+ Next * ListNode
6+ }
7+
8+ func mergeTwoLists (list1 * ListNode , list2 * ListNode ) * ListNode {
9+ if list1 == nil && list2 == nil {
10+ return nil
11+ } else if list1 == nil {
12+ return list2
13+ } else if list2 == nil {
14+ return list1
15+ }
16+
17+ newList := & ListNode {}
18+ cur := newList
19+
20+ for {
21+ switch {
22+ case list1 .Val < list2 .Val :
23+ cur .Next = & ListNode {Val : list1 .Val }
24+ list1 = list1 .Next
25+ case list1 .Val > list2 .Val :
26+ cur .Next = & ListNode {Val : list2 .Val }
27+ list2 = list2 .Next
28+ default :
29+ cur .Next = & ListNode {Val : list1 .Val }
30+ list1 = list1 .Next
31+ cur = cur .Next
32+ cur .Next = & ListNode {Val : list2 .Val }
33+ list2 = list2 .Next
34+ }
35+ cur = cur .Next
36+ if list1 == nil && list2 == nil && cur .Next == nil {
37+ break
38+ }
39+
40+ if list1 == nil {
41+ cur .Next = list2
42+ break
43+ }
44+ if list2 == nil {
45+ cur .Next = list1
46+ break
47+ }
48+
49+ }
50+
51+ return newList .Next
52+ }
You can’t perform that action at this time.
0 commit comments