Skip to content

Commit 0462ca4

Browse files
committed
[medium] 61. Rotate List
1 parent a8d125a commit 0462ca4

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

61.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from typing import Optional
2+
3+
# Definition for singly-linked list.
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
def __repr__(self):
10+
return f"{self.val}, {self.next}"
11+
12+
13+
class Solution:
14+
"""My initial naive Solution
15+
16+
With this solution I'm always going to the end then linking it to the head
17+
then breaking the previous link with the last which makes the prev node to be the last one.
18+
"""
19+
20+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
21+
if not head or k == 0:
22+
return head
23+
24+
current = head
25+
i = 0
26+
size = 0
27+
28+
while current:
29+
current = current.next
30+
size += 1
31+
32+
current = head
33+
34+
while i < (k % size):
35+
while current.next and current.next.next:
36+
current = current.next
37+
38+
prev = current
39+
last = current.next
40+
41+
last.next = head
42+
prev.next = None
43+
head = last
44+
45+
i += 1
46+
current = head
47+
48+
return head
49+
50+
51+
class Solution2:
52+
"""My initial naive Solution
53+
54+
With this solution I'm always going to the end then linking it to the head
55+
then breaking the previous link with the last which makes the prev node to be the last one.
56+
"""
57+
58+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
59+
if not head or k == 0:
60+
return head
61+
62+
current = head
63+
size = 1
64+
65+
while current.next:
66+
current = current.next
67+
size += 1
68+
69+
last = current
70+
last.next = head # here the last is linked with the first one
71+
72+
# now we need to interate over the list to count where we're going to break the link
73+
# then the next before breaking gonna be the head
74+
75+
i = 0
76+
current = head
77+
78+
# the number of operation is equal to how many nodes we want to bring from
79+
# back to the init. K can be pretty big. We can get the module of k per the size
80+
# of the list than we have the maximum nodes we can bring to front.frozenset
81+
operations = (k % size)
82+
until = size - operations - 1
83+
84+
# When we have the number of operations we need to think backwards,
85+
# from the last node to the head how many are going to be bring forward?
86+
# Then we can just get the size - number of operations - 1 (0-index-list)
87+
# then we find in what node we should break the .next and set the head
88+
while current and i < until:
89+
current = current.next
90+
i += 1
91+
92+
93+
head = current.next
94+
current.next = None
95+
96+
return head
97+
98+
99+
head = None
100+
current = None
101+
# nums = [1, 2, 3, 4, 5]
102+
nums = [1, 2]
103+
k = 1
104+
105+
for i, val in enumerate(reversed(nums)):
106+
node = ListNode(val=val, next=current)
107+
108+
if i == len(nums) - 1:
109+
head = node
110+
111+
current = node
112+
113+
114+
print(f"LinkedList: {head}")
115+
116+
head = Solution2().rotateRight(head=head, k=k)
117+
118+
print(f"LinkedList: {head}")

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| 1.py | Hash table | <https://leetcode.com/problems/two-sum/> |
44
| 15.py | Two pointers | <https://leetcode.com/problems/3sum/> |
55
| 35.py | Binary Search | <https://leetcode.com/problems/search-insert-position/> |
6+
| 61.py | Linked List / Two pointers | <https://leetcode.com/problems/rotate-list/description//> |
67
| 80.py | Array / Two pointers | <https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/> |
78
| 82.py | Linked List / Two pointers | <https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/> |
89
| 83.py | Linked List / Two pointers | <https://leetcode.com/problems/remove-duplicates-from-sorted-list/> |

0 commit comments

Comments
 (0)