728x90
난이도 : easy
head를 거꾸로 return하는 문제
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
curr = head
while curr:
temp, curr.next = curr.next, prev
prev, curr = curr, temp
return prev
- 이전 node와 현재 node를 각각 prev, curr로 지정
- curr가 None이 될때까지 node와 포인터를 변경함
Runtime : 0ms
Memory : 18.82MB (12.58%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Leetcode / Python] 3174. Clear Digits (0) | 2025.02.12 |
---|---|
[Leetcode / Python] 1910. Remove All Occurrences of a Substring (0) | 2025.02.11 |
[Python] 19. Remove Nth Node From End of List (0) | 2025.02.10 |
[Python] 160. Intersection of Two Linked Lists (0) | 2025.02.09 |
[Python] 142. Linked List Cycle II (0) | 2025.02.08 |