Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 7. 21. 23:45
https://leetcode.com/problems/reverse-linked-list-ii/
Reverse Linked List II - 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 Solution:
def reverseBetween(self, head: Optional[ListNode], m: int, n: int) -> Optional[ListNode]:
if not head:
return None
left, right = head, head
stop = False
def recAndRev(right, m, n):
nonlocal left, stop
if n == 1:
return
right = right.next
if m > 1:
left = left.next
recAndRev(right, m-1, n-1)
if left == right or right.next == left:
stop = True
if not stop:
left.val, right.val = right.val, left.val
left = left.next
recAndRev(right, m, n)
return head
반응형