-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_58.py
63 lines (49 loc) · 1.49 KB
/
Day_58.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
"""
DAY 58 : Reverse a linked list.
https://www.geeksforgeeks.org/reverse-a-linked-list/
QUESTION : Given a linked list of N nodes. The task is to reverse this list.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 10^4
"""
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Linkedlist:
def __init__(self):
self.head = None
def AtEnd(self, newdata):
NewNode = Node(newdata)
if self.head is None:
self.head = NewNode
return
last_ele = self.head
while(last_ele.next):
last_ele = last_ele.next
last_ele.next = NewNode
def reverse_LinkedList(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
def printlist(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
if __name__ == '__main__':
LList = Linkedlist()
enter_more = 'y'
user_input = None
while enter_more=='y' or enter_more == 'Y':
user_input = int(input("Enter Element: "))
enter_more = input("Want to enter more elements(y/n) :")
LList.AtEnd(user_input)
LList.reverse_LinkedList()
LList.printlist()