-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvyaz.py
More file actions
64 lines (57 loc) · 1.56 KB
/
svyaz.py
File metadata and controls
64 lines (57 loc) · 1.56 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
class Node:
def __init__(self, value: int, next: 'Node'=None):
self.value = value
self.next = next
class LinkedList:
def __init__(self):
self.head = None
"""
def __str__(self):
if self.head:
current = self.head
p=[]
p.append(current.value)
while current.next != None:
current = current.next
p.append(current.value)
return 'LinkedList' +' '+str(p)
"""
def insert_before_head(self, newdata):
new = Node(newdata)
# Update the new nodes next val to existing node
new.head = self.head
self.head = new
def add(self, x):
if not self.head :
self.head=Node(x, None)
else:
last = self.head
while last.next != None:
last = last.next
last.next=Node(x,None)
def get_simple(self):
if self.head:
current = self.head
p=[]
p.append(current.value)
while current.next != None:
current = current.next
p.append(current.value)
return p
linked = LinkedList()
n=int(input())
linked.head = Node(input())
for i in range(n-1):
m=input()
linked.add(m)
print(*linked.get_simple())
def reverse(cursor):
reversed_list = None
while cursor:
temp = reversed_list
next_item = cursor.next
reversed_list = cursor
reversed_list.next = temp
cursor = next_item
return reversed_list
head = Node(1, Node(2, Node(3)))