Skip to content

Commit 121fd9b

Browse files
committed
[medium] 61. Rotate List
1 parent a8d125a commit 121fd9b

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

61.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
53+
if not head or k == 0:
54+
return head
55+
56+
current = head
57+
size = 1
58+
59+
while current.next:
60+
current = current.next
61+
size += 1
62+
63+
last = current
64+
last.next = head # here the last is linked with the first one
65+
66+
# now we need to interate over the list to count where we're going to break the link
67+
# then the next before breaking gonna be the head
68+
69+
i = 0
70+
current = head
71+
72+
# the number of operation is equal to how many nodes we want to bring from
73+
# back to the init. K can be pretty big. We can get the module of k per the size
74+
# of the list than we have the maximum nodes we can bring to front.frozenset
75+
operations = (k % size)
76+
until = size - operations - 1
77+
78+
# When we have the number of operations we need to think backwards,
79+
# from the last node to the head how many are going to be bring forward?
80+
# Then we can just get the size - number of operations - 1 (0-index-list)
81+
# then we find in what node we should break the .next and set the head
82+
while current and i < until:
83+
current = current.next
84+
i += 1
85+
86+
87+
head = current.next
88+
current.next = None
89+
90+
return head
91+
92+
93+
head = None
94+
current = None
95+
# nums = [1, 2, 3, 4, 5]
96+
nums = [1, 2]
97+
k = 1
98+
99+
for i, val in enumerate(reversed(nums)):
100+
node = ListNode(val=val, next=current)
101+
102+
if i == len(nums) - 1:
103+
head = node
104+
105+
current = node
106+
107+
108+
print(f"LinkedList: {head}")
109+
110+
head = Solution2().rotateRight(head=head, k=k)
111+
112+
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)