HappyLeetcode36: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.

 

这道题的难度不算特别大,主要是对链表技能的考察。

奉上我写的代码:

class Solution {
 public:
     ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
         ListNode head(0), *result = &head;
         while (l1 && l2) {
             if (l1->val <= l2->val) {
                 result->next = l1;
                 result = cur->next;
                 l1 = l1->next;
             }
             else {
                 result->next = l2;
                 result = cur->next;
                 l2 = l2->next;
             }
         }
         if (l1) result->next = l1;
         if (l2) result->next = l2;
         return head.next;
     }
 };
时间: 2024-10-15 08:45:10

HappyLeetcode36:Merge Two Sorted Lists的相关文章

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list 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. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

[Leetcode][JAVA] Merge Two Sorted Lists &amp; Sort List

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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

[LintCode] Merge Two Sorted Lists 混合插入有序链表

Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Have you met this question in a real interview? Yes Exampl

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l