본문 바로가기
Programming/LeetCode

[LeetCode] Reverse Linked List

by 데이터현 2022. 3. 23.

https://leetcode.com/problems/reverse-linked-list/

 

Reverse Linked List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

연결 리스트를 뒤집으면 된다.

단순한 문젠데 뭔가 실제로 해보려니까 한동안 뇌 정지 왔다. 역시 아직 부족하다

# 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

댓글