【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

题目

      这道题是链表的简单应用,将两个有序链表合成一个有序链表。
       思路是:表一,表二各取两个对象,分别指向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

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



目录
相关文章
|
4月前
leetcode-913:猫和老鼠
leetcode-913:猫和老鼠
52 1
|
7月前
|
算法 Android开发 C++
LeetCode 周赛上分之旅 #49 再探内向基环树
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
61 1
|
4月前
leetcode-419:甲板上的战舰
leetcode-419:甲板上的战舰
19 0
|
9月前
|
算法
【过河卒】回溯算法保姆式解题
【过河卒】回溯算法保姆式解题
53 0
|
9月前
两道智力题
两道智力题
|
10月前
|
算法 Android开发 容器
LeetCode 周赛上分之旅 #35 两题坐牢,菜鸡现出原形
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
62 0
|
12月前
|
算法 图计算 C++
每日算法系列【LeetCode 42】接雨水
每日算法系列【LeetCode 42】接雨水
LeetCode 面试题 16.11. 跳水板
你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。
45 0
|
算法 前端开发 机器人
「LeetCode」剑指Offer-13机器人的运动范围⚡️
「LeetCode」剑指Offer-13机器人的运动范围⚡️
178 0
「LeetCode」剑指Offer-13机器人的运动范围⚡️
|
Windows
LeetCode 训练场:1431. 拥有最多糖果的孩子
LeetCode 训练场:1431. 拥有最多糖果的孩子
81 0