新博文地址:[leetcode]Merge Two Sorted Lists
http://oj.leetcode.com/problems/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 tw
归并两个排好序的链表,主要考察最基本的链表操作,其实跟数组的归并排序没啥区别
一般情况下,有两种解法:时间复杂度都是O(m+n)
一种是在原链表的基础上进行连接,空间复杂度O(1);
另一种是再建一个新的链表以方便存放连接后的结果,空间复杂度为O(m+n),但是这种操作起来要稍稍简单一点点,主要还是看面试官的要求啦。
不废话:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null || l2 == null) { return l1 == null ? l2 : l1; } ListNode mergeLeft = new ListNode(0); ListNode mergeHead = mergeLeft; ListNode l1left = l1; ListNode l2left = l2; while (l1left != null || l2left != null) { if (l1left == null) { mergeLeft.next = l2left; l2left = null; } else if (l2left == null) { mergeLeft.next = l1left; l1left = null; } else { if (l1left.val < l2left.val) { mergeLeft.next = l1left; l1left = l1left.next; } else { mergeLeft.next = l2left; l2left = l2left.next; } mergeLeft = mergeLeft.next; } } return mergeHead.next; }
相关推荐
LeetCode Merge 2 Sorted Lists解决方案
第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers ...21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli
【Leetcode】Merge Two Sorted Lists
leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...
lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest ...Two Sorted ...two ...Merge Two Sorted Lists 合并两个有序链表 lin
leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。...Merge Two Sorted Lists JavaScript O(n)
Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...
Merge Two Sorted Lists 83 Easy Remove Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked ...
lru缓存leetcode 力扣日记 欢迎来到我的 LeetCode 期刊库。 我的每个问题都将包括一个初始计划阶段,在这个阶段我将用我自己的话来定义问题,我将如何解决这个问题,并且所有内容都将首先用伪代码写出来。 当我解决...
leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two ...Two Sorted ...#1:Two ...Two ...#23:Merge ...Sorted Lists
leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic ...Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ
java二叉树算法源码 algorithm-primer algorithm primer - 算法基础、Leetcode编程、剑指offer ...Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I
LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star!...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis
leetcode中国 cu-cafes This ...具体内容为(https://leetcode.com/problems/merge-two-sorted-lists/)。其中包含规范代码以及自己写的代码 [模块]: "Merge Two Sorted Lists" 备注:代码使用 Alibab
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
Merge Two Sorted Lists] [53. Maximum Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is ...
(https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates-from-sorted-array.py) | _O(n)_ | _O(1)_ |...