[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

题意

合并两个有序链表。

分析

很经典的题目,不过知道怎么做后很容易,模拟即可。

有两种做法:

1. 开一个节点做 head 的前节点 (下面的 Python 代码实现)

2. 不开直接做(C++ 代码实现)

代码

C++:

class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
		if (l1 == NULL)
			return l2;
		if (l2 == NULL)
			return l1;

		ListNode *start, *cur;

		if (l1->val < l2->val) {
			cur = start = l1;
			l1 = l1->next;
		} else {
			cur = start = l2;
			l2 = l2->next;
		}
		while (l1 != NULL && l2 != NULL) {
			if (l1->val < l2->val) {
				cur->next = l1;
				cur = l1;
				l1 = l1->next;
			} else {
				cur->next = l2;
				cur = l2;
				l2 = l2->next;
			}
		}
		if (l1 != NULL)
			cur->next = l1;
		else
			cur->next = l2;
		return start;
    }
};

ListNode *l1, *l2, *ll1, *ll2;
int main() {
	int n1, n2;
	Solution s;
	cin >> n1;
	ll1 = l1 = new ListNode(0);
	for (int i = 0; i < n1; i++) {
		l1->next = new ListNode(0);
		l1 = l1->next;
		scanf("%d", &(l1->val));
	}
	cin >> n2;
	ll2 = l2 = new ListNode(0);
	for (int i = 0; i < n2; i++) {
		l2->next = new ListNode(0);
		l2 = l2->next;
		scanf("%d", &(l2->val));
	}
	ListNode *res = s.mergeTwoLists(ll1->next, ll2->next);
	while (res != NULL) {
		cout << res->val << ' ';
		res = res->next;
	}

	return 0;
}

Python:

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

        dummy = ListNode(0)
        cur = dummy
        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
        cur.next = l1 or l2

        return dummy.next
时间: 2024-08-22 16:21:28

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)的相关文章

[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 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 (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: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

[LeetCode 题解]: 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; * ListNode(int x) : val(x), next(N

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

[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