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. pycharm 常用设置

    PyCharm3.0默认快捷键( 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl + Shift ...

  2. bzoj 1491 floyd

    我们用w[i][j]表示i到j的最短路的数量,dis[i][j]表示i到j的最短路,那么我们在floyd的时候,如果dis[i][k]+dis[k][j]==dis[i][j],根据乘法原理我们就w[ ...

  3. BZOJ-3670 动物园 KMP+奇怪的东西

    YveH爷再刷KMP,DCrusher看他刷KMP,跟着两个人一块刷KMP... 3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB ...

  4. 【bzoj1853】 Scoi2010—幸运数字

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...

  5. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  6. javaIO(二)

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...

  7. hihocoder 1154 Spring Outing

    传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...

  8. 最短路之Dijkstra算法

    1. 邻接矩阵 int cost[MAX_V][MAX_V]; //assume cost[u][v]>0 int d[MAX_V]; bool used[MAX_V]; void Dijkst ...

  9. linux经典命令学习

    本文介绍Linux系统的若干经典命令的常用方法. (一)grep 主要用于搜索文件内容,查看是否跟要求的pattern相匹配. 1.grep -l 'boss' *        显示所有包含boss ...

  10. 周期串(Periodic Strings,UVa455)

    解题思路: 对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除 其次,对于最小周期字符串,每位都能对应其后周期字串的每一位, 即 ABC  A ...