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.
解题思路:
新建一个ListNode进行存储即可,JAVA实现如下:
	static public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode listnode=new ListNode(0);
ListNode index=listnode;
while(l1!=null&&l2!=null){
if(l1.val>l2.val){
ListNode temp=l1;
l1=l2;
l2=temp;
}
index.next=new ListNode(l1.val);
index=index.next;
l1=l1.next;
}
if(l1==null)
index.next=l2;
else
index.next=l1;
return listnode.next;
}

C++:

 class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
else if (l2 == NULL)
return l1;
ListNode* start;
if (l1->val < l2->val) {
start = l1;
l1 = l1->next;
}
else {
start = l2;
l2 = l2->next;
}
ListNode* cur = start;
while (l1 != NULL&&l2 != NULL) {
if (l1->val < l2->val) {
cur->next = l1;
cur = cur->next;
l1 = l1->next;
}
else {
cur->next = l2;
cur = cur->next;
l2 = l2->next;
}
}
if (l1 == NULL)
cur->next = l2;
else
cur->next = l1;
return start;
}
};

【JAVA、C++】LeetCode 021 Merge Two Sorted Lists的更多相关文章

  1. Leetcode 021 Merge Two Sorted Lists

    摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. Oracle自定义函数

    核心提示:函数用于返回特定数据.执行时得找一个变量接收函数的返回值; 语法如下: create or replace function function_name ( argu1 [mode1] da ...

  2. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  3. BZOJ-1045 糖果传递 数学+递推

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...

  4. Eclipse开发Android程序如何在手机上运行

    android开发不论是在真机上调试还是最终发布到真机上都非常简单,过程如下: 1.安装usb驱动 手机要能与电脑相连,当然要安驱动了.效果就是你插入手机,电脑显示驱动已识别.驱动安装的官方教程:ht ...

  5. Spring学习4-面向切面(AOP)之Spring接口方式

    一.初识AOP    关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节       1.AOP:Aspect ...

  6. mysql 一个典型的数据库建表建用户过程

    cmd 命令不接 ";" mysql 命令后接 ";" 1.以管理员身份登录mysql mysql -u root –p 2.选择mysql数据库 use my ...

  7. Max批量导出工具

    Max批量导出工具 http://www.paulneale.com/scripts/batchItMax/batchItMax.htm Scripts Batch It Max: Batch It ...

  8. AngularJS小试牛刀

    总看别人说argularjs,knockoutjs,mvvm模式这些,光看不练假把式,于是便在自己的一个小网站上做了个简单的页面应用了下argularjs ,详细地址见 http://www.591j ...

  9. linux文件系统模拟

    #include "stdio.h" #include <stdlib.h> //#include <conio.h> #include <strin ...

  10. SQLServer 删除所有表和删除所有存储过程

    1.删除所有表 use 数据库declare @tname varchar(8000)set @tname=''select @tname=@tname + Name + ',' from sysob ...