일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- shiba
- 시바견
- iterator
- Substring with Concatenation of All Words
- 43. Multiply Strings
- Python
- Generator
- data science
- Regular Expression
- Class
- 프로그래머스
- kaggle
- Decorator
- Protocol
- DWG
- Python Code
- t1
- Python Implementation
- 밴픽
- 운영체제
- 715. Range Module
- concurrency
- attribute
- 컴퓨터의 구조
- 파이썬
- 315. Count of Smaller Numbers After Self
Archives
- Today
- Total
Scribbling
LeetCode: 21. Merge Two Sorted Lists 본문
Use dummy head to simplify code.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy_head = ListNode()
cur, p1, p2 = dummy_head, l1, l2
while p1 != None and p2 != None:
if p1.val <= p2.val:
cur.next = p1
cur = p1
p1 = p1.next
else:
cur.next = p2
cur = p2
p2 = p2.next
if p1 == None:
cur.next = p2
else:
cur.next = p1
return dummy_head.next
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 350. Intersection of Two Arrays II (0) | 2022.01.10 |
---|---|
LeetCode: 322. Coin Change (0) | 2022.01.07 |
LeetCode: 442. Find All Duplicates in an Array (0) | 2022.01.05 |
LeetCode: 268. Missing Number (0) | 2022.01.04 |
LeetCode: 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2022.01.03 |