We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5328f9f commit d44264fCopy full SHA for d44264f
problems/merge-two-sorted-lists/merge_two_sorted_lists.go
@@ -10,22 +10,14 @@ import . "github.com/openset/leetcode/internal/kit"
10
* }
11
*/
12
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
13
- l := &ListNode{}
14
- t := l
15
- for l1 != nil && l2 != nil {
16
- if l1.Val < l2.Val {
17
- t.Next = l1
18
- l1 = l1.Next
19
- } else {
20
- t.Next = l2
21
- l2 = l2.Next
22
- }
23
- t = t.Next
24
25
if l1 == nil {
26
27
28
+ return l2
+ } else if l2 == nil {
+ return l1
+ }
+ if l1.Val > l2.Val {
+ l1, l2 = l2, l1
29
}
30
- return l.Next
+ l1.Next = mergeTwoLists(l1.Next, l2)
31
0 commit comments