| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 생성형AI
- nlp
- BFS
- 리트코드
- tree
- two-pointer
- graph
- 니트코드
- 투포인터
- GenAI
- binary Tree
- Python
- stratascratch
- 슬라이딩윈도우
- GenerativeAI
- LeetCode
- 파이썬알고리즘
- 파이썬
- codeup
- Python3
- 알고리즘
- heap
- array
- sql코테
- 릿코드
- 코드업
- Stack
- dfs
- Greedy
- SQL
Archives
- Today
- Total
Tech for good
[Leetcode/Linked List]83. Remove Duplicates from Sorted List 본문
IT/Computer Science
[Leetcode/Linked List]83. Remove Duplicates from Sorted List
Diana Kang 2025. 7. 12. 23:54

Solution 1: prev + curr
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = head
curr = None
if head:
curr = head.next
while curr:
if curr.val == prev.val:
curr = curr.next
prev.next = curr
else:
prev = curr
curr = curr.next
return head
Solution 2: Only curr
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
curr = head
while curr and curr.next:
if curr.val == curr.next.val:
curr.next = curr.next.next # Skip duplicate
else:
curr = curr.next
return head'IT > Computer Science' 카테고리의 다른 글
| [Neetcode/Array, Binary Search] Binary Search (0) | 2025.07.15 |
|---|---|
| [Leetcode/Linked List, Hash Table] 3063. Linked List Frequency (0) | 2025.07.13 |
| [Neetcode/Linked List] Reorder Linked List (1) | 2025.07.10 |
| [Neetcode/Linked List] Remove Node From End of Linked List (4) | 2025.07.10 |
| [Leetcode/Linked List, Two Pointers] 876. Middle of the Linked List (0) | 2025.07.09 |