题目:合并两个排序的链表
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
解题思路
参考代码
/*
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
if(pHead1 == null)
return pHead2;
else if(pHead2 == null)
return pHead1;
ListNode pMergedHead = null;
if(pHead1.val < pHead2.val)
{
pMergedHead = pHead1;
pMergedHead.next = Merge(pHead1.next,pHead2);
}
else
{
pMergedHead = pHead2;
pMergedHead.next = Merge(pHead1,pHead2.next);
}
return pMergedHead;
}
}