题解 | #合并两个排序的链表#

合并两个排序的链表

https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
        public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null)return list2;
        if(list2==null)return list1;
        ListNode head1,p;
        ListNode head2,q;
        if(list1.val>list2.val){
              head1= list2;p = list2;
              head2= list1;q = list1;
        }
        else{
              head1= list1;p = list1;
              head2= list2;q = list2;
        }
        ListNode pre=null;
        while(p!=null&&q!=null){
             if(p.val<=q.val){
                  pre = p;
                  p=p.next;
             }
            else {
               head2 = head2.next;
                q.next=p;
                pre.next=q;
                pre=q;
                q=head2;
            } 
        }
        if(q!=null){
            pre.next=q;
        }
        return head1;
    }
}
// 这题有毒,为什么我用python弄不出来,明明对的呀!!!

#我的实习求职记录#
全部评论

相关推荐

06-25 16:25
梧州学院 Java
愿汐_:项目介绍那么长,然而你做了啥就一句话?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务