LeetCode--LinkedList--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 list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
solution##
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(-1);
ListNode p = head, temp = null;
while (l1 != null && l2 != null)
{
if (l1.val > l2.val)
{
temp = l2;
l2 = l2.next;
}
else
{
temp = l1;
l1 = l1.next;
}
p.next = temp;
p = temp;
}
p.next = l1 != null ? l1 : l2;
return head.next;
}
}
总结##
题意是给定两个有序链表l1和l2,将这两个链表合并成一个有序链表。我的思路是构造一个头结点head,然后用一个while循环遍历这两个链表,如果l1.val大于l2.val,则将l2结点用temp变量保存起来,然后l2指向下一个结点,否则,则将l1结点用temp变量保存起来,然后l1指向下一个结点;接着使用尾插法将temp结点插到head链表的尾部;循环结束之后,再将剩下的结点直接插入到head链表尾部,最后返回head.next结点。
还有一种思路是用递归,这里就不细说了。
Notes
1.链表的插入,删除操作最好构造一个额外的头结点,方便操作的统一;
2.此题与两个有序数组的合并是一样的思路;
LeetCode--LinkedList--21.Merge Two Sorted Lists (Easy)的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- [leetcode] 21. Merge Two Sorted Lists (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 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 ...
- 【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 splici ...
- 【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 spli ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
随机推荐
- MySQL的事务隔离级别是什么?
我是平也,这有一个专注Gopher技术成长的开源项目「go home」 背景介绍 想必事务大家都已经非常熟悉了,它是一组SQL组成的一个执行单元,要么全执行要么全不执行,这也是它的一个特性--原子性. ...
- Python 控制流代码混淆简介,加大别人分析你代码逻辑和流程难度
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 王平 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- Personal Photo Management Application
Customer Problems & Needs People may take a large number of photos and their phone don't have en ...
- C - Long Beautiful Integer codeforces 1269C 构造
题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一 ...
- C++学习--编译优化
常量折叠 把常量表达式的值求出来作为常量嵌在最终生成的代码中. 疑问:对于一个很复杂的常量表达式,编译器会算出结果再编译吗?亦或者是把这个表达式完全翻译成机器码,最终留给程序去解决? 分情况: 涉及的 ...
- sftp的用法
linux sftp远程连接命令 sftp -oPort=60001 root@192.168.0.254 使用-o选项来指定端口号. -oPort=远程端口号 sftp> get /var/w ...
- python函数-易错知识点
定义函数: def greet_users(names): #names是形参 """Print a simple greeting to each user in th ...
- Java的数组索引问题
/* 数组操作的两个常见小问题: ArrayIndexOutOfBoundsException:数组索引越界异常 原因:你访问了不存在的索引. NullPointerException:空指针异常 原 ...
- 不论是 Basic Auth 还是 Digest Auth,都会有 Authorization 字段
GET /dir/index.html HTTP/1.0 Host: localhost Authorization: Digest username="Mufasa", real ...
- [Inno Setup] How to create a OuterNotebook/welcome page in the uninstaller
By Martin Prikryl https://stackoverflow.com/questions/42625626/inno-setup-how-to-create-a-outernoteb ...