문제 출처 : leetcode.com/problems/reverse-linked-list/
문제 해석 : 주어진 링크드 리스트를 뒤집어서 반환하는 문제이다.
문제 풀이 : 주어진 링크드 리스트를 하나씩 탐색하면서 새로운 링크드리스트에 연결시켜주어 반환하면 된다.
풀이 코드
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 사이트에 있습니다. 혹시 문제가 되는 부분이 있으면 연락 바랍니다.
'📊알고리즘, 문제풀이 > 📈문제풀이 (PS)' 카테고리의 다른 글
[알고리즘][Python] LeetCode 24 Swap Nodes in Pairs 문제 풀이 (0) | 2021.02.22 |
---|---|
[알고리즘][Python] LeetCode 2 Add Two Numbers 문제 풀이 (0) | 2021.02.22 |
[알고리즘][Python] LeetCode 234 Palindrome Linked List 문제 풀이 (0) | 2021.02.21 |
[알고리즘][Python] 백준 15683 감시 문제 풀이 (2) | 2021.02.19 |
[알고리즘][Python] LeetCode 121 Best Time to Buy and Sell Stock 문제 풀이 (0) | 2021.02.19 |