LeetCode 第21题 合并两个有序链表

/*将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:

输入:1->2->4, 1->3->4输出:1->1->2->3->4->4

Definition for singly-linked list. public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } */
 1 class Solution21 {
 2
 3
 4   /*
 5     思路:迭代  时间O(n)
 6  */
 7   public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 8     if (l1 == null) {
 9       return l2;
10     }
11     if (l2 == null) {
12       return l1;
13     }
14
15     ListNode newList = new ListNode(0);//headNode
16     ListNode nodePos = newList;
17
18     while (l1 != null && l2 != null) {
19       /*
20       将list1中结点放入newList
21        */
22       if (l1.val <= l2.val) {
23         nodePos.next = l1;
24         l1 = l1.next;
25       } else {
26         /*
27         将list2中结点放入newList
28        */
29         nodePos.next = l2;
30         l2 = l2.next;
31       }
32       nodePos = nodePos.next;
33     }
34     nodePos.next = l1 == null ? l2 : l1;
35     return newList.next;
36   }
37
38   /*
39   思路:递归  时间O(n)
40  */
41   public ListNode mergeTwoLists1(ListNode l1, ListNode l2) {
42     if (l1 == null) {
43       return l2;
44     }
45     if (l2 == null) {
46       return l1;
47     }
48     if (l1.val >= l2.val) {
49       l1.next = mergeTwoLists(l1.next, l2);
50       return l1;
51     } else {
52       l2.next = mergeTwoLists(l1, l2.next);
53       return l2;
54     }
55   }
56 }

原文地址:https://www.cnblogs.com/rainbow-/p/10269262.html

时间: 2024-07-31 05:25:13

LeetCode 第21题 合并两个有序链表的相关文章

LeetCode刷题--合并两个有序链表(简单)

题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1 -> 2 -> 4 ,1 -> 3 -> 4 输出:1 -> 1 -> 2 -> 3 -> 4 -> 4 方法 1:递归 思路 特殊的,如果 l1 或者 l2 一开始就是 null ,那么没有任何操作需要合并,所以我们只需要返回非空链表. 终止条件:两条链表分别名为 l1 和 l2,当 l1 为空或 l2 为空时结束 返回值:每一层

每天AC系列(七):合并两个有序链表

1 题目 LeetCode第21题,合并两个有序链表. 2 直接合并 因为已经有序了,类似归并排序中的合并一样,操作不难,直接上代码了. ListNode t = new ListNode(0); ListNode head = t; while(l1 != null && l2 != null) { if(l1.val < l2.val) { t.next = l1; l1 = l1.next; } else { t.next = l2; l2 = l2.next; } t = t

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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 Example: Input: 1->2->4, 1-&g

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n

[leetcode] 21. 合并两个有序链表

21. 合并两个有序链表 两个有序链表合并为一个新的有序链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ans = new ListNode(Integer.MAX_VALUE); ListNode p = ans; while (l1 != null && l2 != null) { if (l1.val < l2.val) { p.next = l1; l

LeetCode 第21题 合并有序链表

(一)题目描述 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 (二)算法描述 1 先创建一个头结点,用

LeetCode - 合并两个有序链表(No.21)

21 - 合并两个有序链表 date: Dec.28th, 2019 platform: windows thinking a recursive method is implemented. code /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListN

算法题——合并两条有序的链表

题目:给定两个已排序的链表,返回合并后的链表. 思路:将链表L2的每个结点插入到链表L1中,时间复杂度为O(m+n),m.n分别为两条链表的长度. 代码: 1 struct ListNode 2 { 3 int value; 4 ListNode *next; 5 ListNode(int v): value(v), next(NULL) 6 { 7 } 8 }; 9 10 ListNode *mergeSortedList(ListNode *L1, ListNode *L2) 11 { 12

leecode刷题(23)-- 合并两个有序链表

leecode刷题(23)-- 合并两个有序链表 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 思路: 这道题我们可以用递归的方法来处理.首先我们可以设置一个临时头节点 head,当链表 l1 和链表 l2 不为空时,对它们进行比较.如果 l1 对应的节点小于或等于 l2 对应的节点,则将 head