-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReversed Linked List.py
72 lines (50 loc) · 1.49 KB
/
Reversed Linked List.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
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
class node:
def __init__(self,data):
self.data = data
self.next = None
class linkedList:
def __init__(self):
self.head = None
def printl(self):
t =self.head
while t:
print(t.data,end = ' ')
t = t.next
def atLast(self,new_data):
new_node = node(new_data)
if self.head is None:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
new_node.next = None
def addElement(self,lis):
lis = lis[::-1]
for i in range(len(lis)):
nl.atLast(lis[i])
def reverse(self):
temp = self.head
lis = []
while temp:
#print(temp.data)
if int(temp.data)%2==0:
lis.append(temp.data)
temp = temp.next
else:
if len(lis)>0:
nl.addElement(lis)
lis = []
nl.atLast(temp.data)
temp = temp.next
if len(lis)>0:
nl.addElement(lis)
l = linkedList()
nl = linkedList()
t = int(input())
arr = list(map(int,input().split()))
for i in range(len(arr)):
l.atLast(arr[i])
l.reverse()
nl.printl()