【LeetCode从零单排】No21.MergeTwoSortedLists

题目

      这道题是链表的简单应用,将两个有序链表合成一个有序链表。
       思路是:表一,表二各取两个对象,分别指向current和next,进行交叉比较排序。
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
      //判断三种null情况
       if(l1==null && l2==null) return l1;
       if(l1==null && l2!=null) return l2;
       if(l1!=null && l2==null) return l1;
       //定义头指针
       if(l1.val<=l2.val){
           //分别对l1和l2取当前和下一节点指针
           ListNode first=l1;
           ListNode second=l1.next;
           ListNode l2_temp=l2;
           ListNode l2_second=l2.next;
           //l2插向l1
           while(l2_temp!=null){
               if(second!=null){
               if(l2_temp.val>=first.val && l2_temp.val<=second.val){
                  first.next=l2_temp;
                  l2_temp.next=second;
                  first=first.next;
                  second=first.next; 
                  //这个try catch是指当l2_second=null时,l2_second.next是空指针,这时已经排序结束,所以return
                 try{
                  l2_temp=l2_second;                  
                  l2_second=l2_second.next;}
                 catch (Exception e){
                	 return l1;
                 }
                                 
               }
               else{
            	      first=first.next;
                   second=first.next;
               }}
               else{
                   first.next=l2_temp;
                  return l1;
               }
              
           }
           return l1;
       }
       else{
    	   ListNode first=l2;
           ListNode second=l2.next;
           ListNode l1_temp=l1;
           ListNode l1_second=l1.next;
           while(l1_temp!=null){
               if(second!=null){
               if(l1_temp.val>=first.val && l1_temp.val<=second.val){
                  first.next=l1_temp;
                  l1_temp.next=second;
                  first=first.next;
                  second=first.next;                  
                 try{
                  l1_temp=l1_second;                  
                  l1_second=l1_second.next;}
                 catch (Exception e){
                	 return l2;
                 }
                                 
               }
               else{
            	      first=first.next;
                   second=first.next;
               }}
               else{
                   first.next=l1_temp;
                  return l2;
               }
              
           }
           return l2;
           
       }
       
    }
    
}


/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值