Linked Lists Merge¶
Table of Contents¶
- 2. Add Two Numbers (Medium)
- 445. Add Two Numbers II (Medium)
- 2816. Double a Number Represented as a Linked List (Medium)
- 21. Merge Two Sorted Lists (Easy)
- 369. Plus One Linked List (Medium) 👑
- 1634. Add Two Polynomials Represented as Linked Lists (Medium) 👑
2. Add Two Numbers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, math, recursion
- Represent the sum of two numbers as a linked list.
2. Add Two Numbers - Python Solution
from typing import Optional
from template import ListNode
# Linked List
def addTwoNumbers(
l1: Optional[ListNode], l2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode()
cur = dummy
carry = 0
while l1 or l2:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
carry, val = divmod(v1 + v2 + carry, 10)
cur.next = ListNode(val)
cur = cur.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry:
cur.next = ListNode(val=carry)
return dummy.next
l1 = ListNode.create([2, 4, 3])
l2 = ListNode.create([5, 6, 4])
print(addTwoNumbers(l1, l2)) # 7 -> 0 -> 8
2. Add Two Numbers - C++ Solution
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy;
ListNode* cur = &dummy;
int carry = 0;
while (l1 || l2 || carry) {
if (l1) {
carry += l1->val;
l1 = l1->next;
}
if (l2) {
carry += l2->val;
l2 = l2->next;
}
cur->next = new ListNode(carry % 10);
cur = cur->next;
carry /= 10;
}
return dummy.next;
}
};
445. Add Two Numbers II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, math, stack
2816. Double a Number Represented as a Linked List¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, math, stack
- Given a number represented as a linked list, double it and return the resulting linked list.
2816. Double a Number Represented as a Linked List - Python Solution
from typing import Optional
from template import ListNode
def doubleIt(head: Optional[ListNode]) -> Optional[ListNode]:
def twice(node):
if not node:
return 0
doubled_value = node.val * 2 + twice(node.next)
node.val = doubled_value % 10
return doubled_value // 10
carry = twice(head)
if carry:
head = ListNode(val=carry, next=head)
return head
head = ListNode.create([1, 2, 3, 4])
print(head)
# 1 -> 2 -> 3 -> 4
print(doubleIt(head))
# 2 -> 4 -> 6 -> 8
21. Merge Two Sorted Lists¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: linked list, recursion
- Merge the two lists into one sorted list.
21. Merge Two Sorted Lists - Python Solution
from typing import Optional
from template import ListNode
# Linked List
def mergeTwoLists(
list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode()
cur = dummy
while list1 and list2:
if list1.val < list2.val:
cur.next = list1
list1 = list1.next
else:
cur.next = list2
list2 = list2.next
cur = cur.next
if list1:
cur.next = list1
elif list2:
cur.next = list2
return dummy.next
list1 = ListNode.create([1, 2, 4])
list2 = ListNode.create([1, 3, 4])
print(mergeTwoLists(list1, list2))
# 1 -> 1 -> 2 -> 3 -> 4 -> 4
21. Merge Two Sorted Lists - C++ Solution
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy;
ListNode* cur = &dummy;
while (list1 && list2) {
if (list1->val < list2->val) {
cur->next = list1;
list1 = list1->next;
} else {
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}
cur->next = list1 ? list1 : list2;
return dummy.next;
}
};
369. Plus One Linked List¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, math
1634. Add Two Polynomials Represented as Linked Lists¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, math, two pointers