-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrearrange_LL.py
More file actions
93 lines (80 loc) · 1.94 KB
/
rearrange_LL.py
File metadata and controls
93 lines (80 loc) · 1.94 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def rearrangeLinkedList(head, k):
# Write your code here.
k_small = []
k_nodes = []
k_large = []
while head:
print(head.value)
if head.value == k:
k_nodes.append(head)
elif head.value < k:
k_small.append(head)
else:
k_large.append(head)
prev = head
head = head.next
prev.next = None
print('small: ' ,k_small)
print()
print('equals:' ,k_nodes)
print()
print('larger:' ,k_large)
a = joinLists(k_small)
b = joinLists(k_nodes)
c = joinLists(k_large)
ans = None
ans = joinAll(a,b,c)
head = ans
while head:
print(head.value, end = '')
head = head.next
return ans
def joinLists(node_list):
if len(node_list):
og = node_list[0]
head = og
i = 1
while i < len(node_list):
head.next = node_list[i]
head = node_list[i]
i += 1
return og
def joinAll(a,b,c):
if a:
head = a
prev = head
while head:
prev = head
head = head.next
if b:
prev.next = b
else:
prev.next = c
if b:
head = b
prev = head
while head:
prev = head
head = head.next
if c:
prev.next = c
if a:
return a
elif b:
return b
else:
return c
# This is the class of the input linked list.
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None
# problem 3 -> 0 -> 5 -> 2 -> 1 -> 4
# result 0 -> 2 -> 1 -> 3 -> 5 -> 4
my_LL = LinkedList(6)
my_LL.next = LinkedList(0)
my_LL.next.next = LinkedList(5)
my_LL.next.next.next = LinkedList(2)
my_LL.next.next.next.next = LinkedList(1)
my_LL.next.next.next.next.next = LinkedList(4)
print(rearrangeLinkedList(my_LL, 3))-