-
Notifications
You must be signed in to change notification settings - Fork 0
/
337.py
45 lines (32 loc) · 835 Bytes
/
337.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Problem:
Given a linked list, uniformly shuffle the nodes. What if we want to prioritize space
over time?
"""
from random import randint
from DataStructures.LinkedList import LinkedList
def shuffle(ll: LinkedList) -> LinkedList:
length = len(ll)
if length in (0, 1):
return ll
for _ in range(length):
pos1, pos2 = randint(0, length - 1), randint(0, length - 1)
node1, node2 = ll.head, ll.head
for _ in range(pos1):
node1 = node1.next
for _ in range(pos2):
node2 = node2.next
node1.val, node2.val = node2.val, node1.val
return ll
if __name__ == "__main__":
ll = LinkedList()
for i in range(1, 6):
ll.add(i)
print(ll)
shuffle(ll)
print(ll)
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(1)
"""