📊알고리즘, 문제풀이/📈문제풀이 (PS)

[알고리즘][Python] LeetCode 206 Reverse Linked List 문제 풀이

Written by Donghak Park

문제 출처 : 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


문제 해석 : 주어진 링크드 리스트를 뒤집어서 반환하는 문제이다.

 

문제 풀이 : 주어진 링크드 리스트를 하나씩 탐색하면서 새로운 링크드리스트에 연결시켜주어 반환하면 된다.


풀이 코드

class ListNode:
    val = 0
    next = None

    def __init__(self, val = 0, next = None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:

        now = head
        temp = None
        while now:
            nx, now.next = now.next, temp
            temp, now = now, nx

        return temp

author : donghak park
contact : donghark03@naver.com

## 문제의 저작권은 LeetCode 사이트에 있습니다. 혹시 문제가 되는 부분이 있으면 연락 바랍니다.