【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.合并过程中,一个链表已经走到了末尾,即移动到了空指针,但另一个链表还剩下一段,则把剩下的这段接到合并后列表的最后

剩下都就是合并过程中的指针的值的比较而已。

代码:

C++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
		/* 有一个为空,合并后为非空的另一个链表 */
        if(l1 == NULL)
            return l2;

        if(l2 == NULL)
            return l1;

        ListNode *head = NULL, *cur = NULL;

		/* 初始化头指针 */
        if(l1->val < l2->val)
        {
            head = l1;
            l1 = l1->next;
        }
        else
        {
            head = l2;
            l2 = l2->next;
        }
		/* 当前指针 */
        cur = head;

		/* 要两个链表都非空才可以一直往后合并 */
        while(l1 && l2)
        {
            if(l1->val < l2->val)
            {
                cur->next = l1;
                cur = cur->next;
                l1 = l1->next;
            }
            else
            {
                cur->next = l2;
                cur = cur->next;
                l2 = l2->next;
            }
        }

		/* 其中一个为空之后,把另一个非空的链表接到合并后链表的最后 */
        if(l1) cur->next = l1;
        else if(l2) cur->next = l2;

        return head;

    }
};

Python:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param two ListNodes
    # @return a ListNode
    def mergeTwoLists(self, l1, l2):

        if not l1:
            return l2
        if not l2:
            return l1

        if l1.val < l2.val:
            head = l1
            l1 = l1.next
        else:
            head = l2
            l2 = l2.next

        cur = head
        while l1 and l2:
            if l1.val < l2.val:
                cur.next = l1
                l1 = l1.next
            else:
                cur.next = l2
                l2 = l2.next
            cur = cur.next

        if l1:
            cur.next = l1
        elif l2:
            cur.next = l2

        return head
时间: 2024-10-12 07:27:35

【LeetCode】Merge Two Sorted Lists的相关文章

【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(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)

【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

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. AC代码如下: ListNode* merge(ListNode* l1,ListNode* l2,int f1,int f2) { if(f1<f2) return merge(l2,l1,f2,f1); //f1

【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

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

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