【leetcode】Merge Two Sorted Lists(easy)

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 fakehead(0);
        ListNode *p1 = l1;
        ListNode *p2 = l2;
        ListNode *pnew = &fakehead;

        while(p1 != NULL && p2 != NULL)
        {
            if(p1->val < p2->val)
            {
                pnew->next = p1;
                p1 = p1->next;
            }
            else
            {
                pnew->next = p2;
                p2 = p2->next;
            }
            pnew = pnew->next;
        }
        if(p1 != NULL)
        {
            pnew->next = p1;
        }
        if(p2 != NULL)
        {
            pnew->next = p2;
        }

        return fakehead.next;
    }
};
时间: 2024-10-14 00:05:03

【leetcode】Merge Two Sorted Lists(easy)的相关文章

【leetcode】Merge k Sorted Lists (归并排序)

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个有序链表,最后返回一个总的有序链表,分析并描述其复杂度.该题的实质为归并排序,平均时间复杂度为O(NlogN). Java AC代码: public class Solution { public ListNode mergeKLists(List<ListNode> list

Leetcode 21. Merge Two Sorted Lists(easy)

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-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

【LeetCode】Merge k Sorted Lists 解题报告

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并几个有序链表为一个,分析算法复杂度. [分治] 直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合并为newlist3,newlist3再和list4合并为newlist4--依次类推:2)list1和list2合并为li

【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

【LeetCode】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.合并过程中,一个链表已经走到了末尾,即移动到了空指针,但另一个链表还

【leetcode】Contains Duplicate &amp; Rectangle Area(easy)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 思路:简单题用set bool cont

【leetcode】Remove Linked List Elements(easy)

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 思路:简单题. //Definition for singly-linked list. struct ListNod

leetcode_21题——Merge Two Sorted Lists(链表)

Merge Two Sorted Lists Total Accepted: 61585 Total Submissions: 188253My Submissions Question Solution 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. Hide

【Leetcode】【Hard】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 1.先取出k个list的首元素,每个首元素是对应list中的最小元素,组成一个具有k个结点的最小堆:o(k*logk) 2.此时堆顶元素就是所有k个list的最小元素,将其pop出,并用此最小元素所在list上的下一个结点(如果存在)填充堆顶,并执行下滤操作,重新构建堆.o(logk) 3