728x90
LeetCode-206
Reverse Linked List : Reverse Linked List
note : ์ฐ๊ฒฐ ๋ฆฌ์คํธ ์ญ์์ผ๋ก ์ ๋ ฌ
:::python
# 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]:
def reverse(node: ListNode, prev: ListNode = None):
if not node:
return prev
next, node.next = node.next, prev
return reverse(next, node)
return reverse(head)
Result : 36ms Memory: 20.4mb
๋ฐ์ํ
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode 24] Swap Nodes in Pairs (0) | 2023.03.20 |
---|---|
[leetcode-2] Add Two Numbers (0) | 2023.03.17 |
[leetcode-21] Merge Two Sorted Lists (0) | 2023.03.12 |
[leetcode 234] Palindrom Linked List (0) | 2023.03.11 |
[leetcode-121] Best Time to Buy and Sell Stock (0) | 2023.03.09 |