본문 바로가기
Python

LeetCode 206. Reverse Linked List - Python

by Air’s Big Data 2020. 8. 11.

LeetCode 206. Reverse Linked List - Python

 

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Solution:

class Solution(object):
    def reverseList(self, head):
        prev = None
        curr = head
        
        while curr!= None:
            tmp = curr.next
            curr.next = prev
            prev = curr
            curr = tmp
            
        return prev

 

(참고 사이트)

링크드리스트 역순 재배열 : https://www.youtube.com/watch?v=gf_BiXt4YlQ

댓글