21 Merge Two Sorted Lists(两链表归并排序Easy)
题目意思:对两个递增链表进行归并排序
思路:没什么好说的,二路归并
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode();
ListNode* p=head;
while(l1&&l2){
if(l1->val<=l2->val){
p->next=l1;
l1=l1->next;
p=p->next;
}
else{
p->next=l2;
l2=l2->next;
p=p->next;
}
}
if(l1)p->next=l1;
if(l2)p->next=l2;
return head->next; //第一个节点去掉
}
};
21 Merge Two Sorted Lists(两链表归并排序Easy)的更多相关文章
- 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 t ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- [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 t ...
随机推荐
- 数据结构(虚树,动态规划):HNOI 2014 世界树
Hnoi2014 世界树 Description 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条里,公平 ...
- 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life
John is a Chief Executive Officer at a privately owned medium size company. The owner of the company ...
- Delphi 重写控件的一个例子。
unit DBGridEx; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ...
- ZOJ 3675 Trim the Nails
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 DP+状态压缩. http://www.cnblogs.com/dgsr ...
- supesite 相关 修改
1.修改 站点名称及logo http://faq.comsenz.com/viewnews-594 2.supesite 如何 收录论坛 http://faq.comsenz.com/viewnew ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- arcgis数据文件使用
dem数据打开,保存,使用 打开
- [Sequence Alignment Methods] Dynamic time warping (DTW)
本系列介绍几种序列对齐方法,包括Dynamic time warping (DTW),Smith–Waterman algorithm,Cross-recurrence plot Dynamic ti ...
- [AS/400] Control Language(CL) 基本概念
本文内容源于 Go4AS400 简介 AS400 Control Language(CL) 是由指令(Command)组合而成,用于控制操作和调用系统功能.在 CL 程序中,指令用于和系统 OS400 ...
- IntelliJ IDEA安装 一些配置
idea 配置修改 本篇 参考https://blog.liyang.io/234.html. 1.修改IDEA菜单的字体大小: 单击File | Project Structure菜单项,打开Pro ...