(LeetCode 21)Merge Two Sorted Lists
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.
题目要求:
合并两个有序链表
注意:
不能开辟新的结点空间
解题思路:
1、归并排序,创建一个新的头结点,从头到尾分别遍历两个链表,并依次比较其大小关系,每次将头指针指向小的那个。
2、递归思想(对于为改变链表结构的题目,一般也可以采用递归的方法)
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ // Merge combination method
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode head();
ListNode *lst;
lst=&head;
while(l1 && l2){
if(l1->val<=l2->val){
lst->next=l1;
l1=l1->next;
}
else{
lst->next=l2;
l2=l2->next;
}
lst=lst->next;
}
if(l1)
lst->next=l1;
if(l2)
lst->next=l2;
return head.next;
}
}; // Recursive method
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1; if(l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l2->next, l1);
return l2;
}
}
(LeetCode 21)Merge Two Sorted Lists的更多相关文章
- LeetCode(21)Merge Two Sorted Lists
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...
- 【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【LeetCode练习题】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode LinkList 23. Merge k Sorted Lists
这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...
- [LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)
①英文题目 Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...
- LeetCode OJ:Merge k Sorted Lists(归并k个链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类 ...
随机推荐
- [COGS2479]偏序
[COGS2479]偏序 题目大意: \(n(n\le50000)\)个四元组,求四维偏序. 思路: CDQ分治套CDQ分治套树状数组. 细节: 第二层CDQ之前要备份数组\(a\),否则第二层CDQ ...
- 20172333 2017-2018-2 《Java程序设计》第6周学习总结
20172333 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容 1.数组的基本用法,如数组的定义:int[该数组类型] name = new int[X]X为数 ...
- Codeforces Round #294 (Div. 2)C - A and B and Team Training 水题
C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...
- oracle_创建表空间、用户、授权等步骤
--创建表空间(在system用户下才可以创建) //connect system/manager as sysdba:切换到system用户 create tablespace tz datafil ...
- 推荐一个简洁优雅的博客系统,farbox
这是我用farbox搞的一个博客:http://www.jsnull.com/ 特点: 1.无数据库,数据存在dropbox里,需要自己注册一个dropbox帐号 2.静态文本文件即是文章,可以在任何 ...
- Oracle问题诊断过程常用SQL
--查看临时表空间使用情况select tablespace_name ,sum(size_mb),sum(used_mb),round(sum(used_mb)/sum(size_mb),2) fr ...
- [置顶] iOS中让省略号垂直居中
在显示等待框时,一般要求在提示信息后面加个省略号,但中文输入法下输入的省略号是在底部对齐,但中 文的习惯是省略号垂直居中对齐,最后找到下面这个方法来显示垂直居中的省略号: 中文和英文输入法下一样: o ...
- android 4.4删除短信
android 4.4之后非默认的短信应用已经没有办法删除短信了.像以前那样用如下方法是不会没法删除短信的(即使在xml中配置了短信的读写权限),同时也不会有报错或其他提示. public void ...
- <jsp:directive.page import=""/>的用法和解释
<jsp:directive.page import="zero.space.ch03.BookBean"/> 相当于 <%@ page import ...
- AlertView动画
AlertView动画 效果 源码 https://github.com/YouXianMing/Animations // // AbstractAlertView.h // Animations ...