일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- attribute
- Protocol
- shiba
- 프로그래머스
- Python Code
- Convert Sorted List to Binary Search Tree
- kaggle
- 43. Multiply Strings
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- 컴퓨터의 구조
- Decorator
- 715. Range Module
- 30. Substring with Concatenation of All Words
- Class
- iterator
- concurrency
- 파이썬
- DWG
- Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- Regular Expression
- Generator
- 시바견
- t1
- data science
- Python Implementation
- 운영체제
- Python
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 |