Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 대학생
- sort
- 코테
- Python
- adaptive remeshing
- Database
- Leet Code
- mysql
- coding
- DS
- db
- SQL
- 컴퓨터공학
- Mesh
- Data_Structure
- 데이터
- 데이터베이스
- CNN
- 오블완
- 컴퓨터사이언스
- LeetCode
- code
- 티스토리챌린지
- meshgraphnet
- 개발자
- GNN
- 코딩테스트
- 데베
- 자료구조
- CS
Archives
- Today
- Total
sy1214ei 님의 블로그
[Leet Code] 21. Merge Two Sorted Lists - Python 본문
level : Easy
Topics : Linked List | Recursion
https://leetcode.com/problems/merge-two-sorted-lists/
#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, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode() # 더미 노드 생성
current = dummy
# list1 & list2 모두 남아있는 동안
while list1 and list2:
if list1.val < list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next
# 남은 노드 처리
current.next = list1 if list1 else list2
return dummy.next
# Time Complexity : O(n+m)
# Space Complexity : O(1)

'[Coding]' 카테고리의 다른 글
| [Leet Code] 1. Two Sum - Python (0) | 2024.12.03 |
|---|---|
| [Leet Code] 9. Palindrome Number - Python (0) | 2024.11.30 |
| [Leet Code] 645. Set Mismatch - Python (0) | 2024.11.28 |
| [Leet Code] 58. Length of Last Word - Python (4) | 2024.11.26 |
| [Leet Code] 747. Largest Number At Least Twice of Others - Python (0) | 2024.11.25 |