【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;

       }

    }

}

代码下载:https://github.com/jimenbian/GarvinLeetCode

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

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

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

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

时间: 2024-08-30 02:07:19

【LeetCode从零单排】No21.MergeTwoSortedLists的相关文章

【LeetCode从零单排】No118 Pascal&#39;s Triangle

题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

【LeetCode从零单排】No70.ClimbingStairs

题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这道题一共有三个方法实现(我想到的): 1.递归(对应代码climbStairs) 如果楼梯有n层,我们回退到n-

【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For exam

【LeetCode从零单排】No.7 Reverse Integer

前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似简单,却费了很大周折. 题目如下: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 刚看到这道题,首先蹦出的想法是把整数转换为字符串,然后前后位置换下再转回int型,实事证明这样是不可取的,因

【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest su

【LeetCode从零单排】No67.AddBinary

题目 Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 代码 public class Solution { public String addBinary(String a, String b) { String lon= a.length()-b.length()>=0 ?

【LeetCode从零单排】No58.Length of Last Word

题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

【LeetCode从零单排】No38.CountAndSay

题目 The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given

【LeetCode从零单排】No36 Valid Sudoku

题目 判断数独是否成立的一道题,看的是某大神的答案,写的太漂亮了. Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Not