No.21 Merge Two Sorted List

No.21 Merge Two Sorted List

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.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
12     {
13         if(l1 == NULL)
14             return l2;
15         if(l2 == NULL)
16             return l1;
17
18         ListNode *head, *current;
19         //使用头结点的方法:ListNode helper = new ListNode(0); helper.next = head1;
20         //他们都用的是将l2的节点插入l1中的方法
21
22         if(l1->val <= l2->val)
23         {
24             head = l1;
25             l1 = l1->next;
26         }
27         else
28         {
29             head = l2;
30             l2 = l2->next;
31         }
32
33         current = head;
34
35         while(l1 && l2)
36         {
37             if(l1->val <= l2->val)
38             {
39               current->next = l1;
40               l1 = l1->next;
41             }
42             else
43              {
44               current->next = l2;
45               l2 = l2->next;
46             }
47             current = current->next;
48         }
49
50         if(l1)
51             current->next = l1;
52         if(l2)
53             current->next = l2;
54
55         return head;
56     }
57 };
时间: 2024-08-03 23:54:07

No.21 Merge Two Sorted List的相关文章

leetCode 21. Merge Two Sorted Lists 合并链表

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. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://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 th

21. Merge Two Sorted Lists-leetcode-java

[原来在SAE的blog上,都转到CSDN了..] 21. Merge Two Sorted Lists-leetcode-java 发表于 2016/02/05 题目 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. 要求合并两个排好序的链表 public cla

21. Merge Two Sorted Lists(js)

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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4题意

[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

leedCode练题——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 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-&g

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 fir

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. 分析: 就是使用两个指针,让较小的归并到新的list上: 代码: 方法一: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2

【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. 3 * public class ListNode