题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution {
public ListNode Merge(ListNode pHead1, ListNode pHead2) {
// write code here
ListNode t = new ListNode(-1);
ListNode tail = t;
while (pHead1 != null && pHead2 != null) {
if (pHead1.val < pHead2.val) {
tail.next = pHead1;
pHead1 = pHead1.next;
} else {
tail.next = pHead2;
pHead2 = pHead2.next;
}
tail = tail.next;
}
tail.next = pHead1 == null ? pHead2 : pHead1;
return t.next;
}
}
美的集团公司福利 814人发布