0%

leetCode-21:Merge Two Sorted Lists

问题描述

给定两个有序链表,要求将其合并成一个有序链表返回。题目链接:**点我**

样例输入输出

输入:1->2->4, 1->3->4

输出:1->1->2->3->4->4

输入:1->2->3->4, 1

输出:1->1->2->3->4

问题解法

直接用归并法即可。代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
if (l1 == null)
{
return l2;
}

if (l2 == null)
{
return l1;
}

ListNode p1 = l1;
ListNode p2 = l2;
if (p1.val > p2.val)
{
p1 = l2;
p2 = l1;
}

ListNode head = p1;
while (p2 != null)
{
while (p1.next != null && p1.next.val < p2.val)
{
p1 = p1.next;
}

ListNode temp = p1.next;
p1.next = p2;
p1 = p2;
p2 = temp;
}

return head;
}
}