https://leetcode.com/problems/reverse-linked-list/
연결 리스트를 뒤집으면 된다.
단순한 문젠데 뭔가 실제로 해보려니까 한동안 뇌 정지 왔다. 역시 아직 부족하다
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
node, prev = head, None
while node:
next, node.next = node.next, prev
node, prev = next, node
return prev
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] Boats to Save People (0) | 2022.03.24 |
---|---|
[LeetCode] Add Two Numbers (0) | 2022.03.23 |
[LeetCode] Broken Calculator (0) | 2022.03.23 |
[LeetCode] Merge Two Sorted Lists (0) | 2022.03.23 |
[LeetCode] Palindrome Linked List (0) | 2022.03.23 |
댓글