-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_practice.py
More file actions
60 lines (45 loc) · 1.19 KB
/
linked_list_practice.py
File metadata and controls
60 lines (45 loc) · 1.19 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
class Linked_list:
def __init__(self, head_node):
self.head = head_node # Instance variable
self.length = 0
# Instance method
def get_length (self):
count = 1
current = self.head
while current.next!=None:
current = current.next
count+=1
return count
def __str__(self):
return f"Linked list(head='{self.head}', length={self.get_length()})"
class Node:
#class variable: shared by all instances
def __init__(self,value,next):
self.value = value
self.next = next
def __repr__(self):
return f"value={self.value}, next={self.next}"
lst = ["a","b","c","d","e","f"]
ll = Linked_list(None)
previous = Node(-1,None)
for ind,i in enumerate(lst):
current_node = Node(i,None)
if ind == 0:
ll.head = current_node
else:
previous.next = current_node
previous = current_node
n = ll.get_length()//2
a = ll.head
for _ in range(n-1):
a = a.next
b = a.next
previous = a
current = a.next
for _ in range(n):
next = current.next
current.next = previous
previous,current = current,next
a.next = previous
b.next = None
print(str(ll))