021. Merge Two Sorted Lists

 1 if (l1 == nullptr) return l2;
 2     if (l2 == nullptr) return l1;
 3     ListNode dummy(-1);
 4     ListNode* pHead = &dummy;
 5     while (l1 && l2) {
 6         if (l1->val < l2->val) {
 7             pHead->next = l1;
 8             l1 = l1->next;
 9         }
10         else {
11             pHead->next = l2;
12             l2 = l2->next;
13         }
14         pHead = pHead->next;
15     }
16     pHead->next = l1 == nullptr ? l2 : l1;
17     return dummy.next;
时间: 2024-10-18 04:14:02

021. Merge Two Sorted Lists的相关文章

Leetcode 021 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. Java: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Li

Java for LeetCode 021 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. 解题思路: 新建一个ListNode进行存储即可,JAVA实现如下: static public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 021.Merge_Two_Sorted_Lists (Easy) 链接: 题目:https://oj.leetcode.com/problems/merge-two-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 合并两个有序链表. 分

[Leetcode][021] Merge Two Sorted Lists (Java)

题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 Linked List中的变形.不多说,直接上代码了 1 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 2 ListNode dummyHead = new ListNode(-1); 3 ListNode node = dum

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

【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个链表中有的