面试题25:合并两个排序的链表

题目:输入两个递增排序的链表,合并这两个链表并使新链表中的节点依然是排序的

public class MergeNode {
    public ListNode Merge(ListNode pHead1,ListNode pHead2){
        if(pHead1==null) return pHead2;
        if(pHead2==null) return pHead1;
        ListNode pMergeNode = null;
        if(pHead1.val<pHead2.val){
            pMergeNode = pHead1;
            pMergeNode.next = Merge(pHead1.next,pHead2);
        }
        else{
            pMergeNode=pHead2;
            pMergeNode.next=Merge(pHead1,pHead2.next);
        }
        return pMergeNode;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容