forked from onlybooks/python-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19-1.py
More file actions
25 lines (21 loc) · 699 Bytes
/
19-1.py
File metadata and controls
25 lines (21 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
# 예외 처리
if not head or m == n:
return head
root = start = ListNode(None)
root.next = head
# start, end 지정
for _ in range(m - 1):
start = start.next
end = start.next
# 반복하면서 노드 차례대로 뒤집기
for _ in range(n - m):
tmp, start.next, end.next = start.next, end.next, end.next.next
start.next.next = tmp
return root.next