Skip to main content

Python - Reversing a Singly Linked List

Program to vreate a Singly Linked List and then reverse its order.
Run the program here: https://repl.it/@VinitKhandelwal/ReverseSinglyLinkedList
class SinglyLinkedList:

def __init__(self):
self.reset()
def reset(self):
self.length = 0
self.head = None
self.tail = None

def first_node(self, new_node):
self.head = new_node
self.tail = self.head
self.length += 1

def append_non_first(self, new_node):
self.tail.front_pointer = new_node
self.tail = new_node
self.length += 1

def prepend_non_first(self, new_node):
new_node.front_pointer = self.head
self.head = new_node
self.length += 1

def append(self, value):
new_node = Node(value)
if self.length == 0:
self.first_node(new_node)
else:
self.append_non_first(new_node)

def prepend(self, value):
new_node = Node(value)
if self.length == 0:
self.first_node(new_node)
else:
self.prepend_non_first(new_node)

def insert_at(self, index, value):
new_node = Node(value)
if self.length == 0:
self.first_node(new_node)
elif index > self.length+1:
print("Index not available. Appended at the end.")
self.append_non_first(new_node)
else:
temp = self.head
if index == 0:
self.prepend_non_first(new_node)
elif index == self.length+1:
append_non_first(new_node)
else:
for i in range(0, index):
prev_temp = temp
temp = temp.front_pointer
prev_temp.front_pointer = new_node
new_node.front_pointer = temp
self.length += 1

def pop_front_non_first(self):
self.head = self.head.front_pointer
self.head.back_pointer = None
self.length -= 1

def pop_last_non_first(self):
prev_temp = self.head
for i in range(1, self.length):
temp = prev_temp.front_pointer
prev_temp = temp
prev_temp.front_pointer = None
self.length -= 1

def popfirst(self):
if self.length < 2:
self.reset()
else:
self.pop_front_non_first()

def poplast(self):
if self.length < 2:
self.reset()
else:
self.pop_last_non_first()

def delete_at(self, index):
if self.length < 2:
self.reset()
elif index == 0:
self.pop_front_non_first()
elif index == self.length-1:
self.pop_last_non_first()
elif index < self.length and index > 0:
temp = self.head
for i in range(0, index):
prev_temp = temp
temp = prev_temp.front_pointer
prev_temp.front_pointer = temp.front_pointer
self.length -= 1

def delete(self, value):
if self.length < 2:
if self.head.value == value:
self.reset()
else:
if self.head.value == value:
self.pop_front_non_first()
if self.tail.value == value:
self.pop_last_non_first()
prev_temp = self.head
for i in range(1, self.length-1):
temp = prev_temp.front_pointer
if temp.value == value:
prev_temp.front_pointer = temp.front_pointer
self.length -= 1
prev_temp = temp
def view(self):
print("View")
temp = None
arr = []
for i in range(0, self.length):
if temp == None:
temp = self.head
else:
temp = temp.front_pointer
if temp is not None:
arr.append(temp.value)
print(arr)

def reverse(self):
if self.length > 1:
second = self.head
first = second.front_pointer
second.front_pointer = None
for i in range(1, self.length-1): # 1,2,3 ,4,5,6,7,8
temp = first.front_pointer # next = <10><3><2>
first.front_pointer = second # <4-10><10-4><3-10>
second = first # prev_temp = <4><10>
first = temp # temp = <10><3>
temp = self.head # temp = <10>
self.head = self.tail # self.head = <10>
self.tail = temp # self.tail = <10>
self.head.front_pointer = second


class Node:
def __init__(self, value, front_pointer=None):
self.value = value
self.front_pointer = front_pointer


obj = SinglyLinkedList()
obj.view()
obj.prepend(10)
obj.view()
obj.append(1)
obj.view()
obj.append(10)
obj.view()
obj.prepend(2)
obj.view()
obj.insert_at(3, 7)
obj.view()
obj.prepend(3)
obj.view()
obj.prepend(10)
obj.view()
obj.prepend(4)
obj.view()
obj.prepend(10)
obj.view()
obj.reverse()
obj.view()
OUTPUT
View
[]
View
[10]
View
[10, 1]
View
[10, 1, 10]
View
[2, 10, 1, 10]
View
[2, 10, 1, 7, 10]
View
[3, 2, 10, 1, 7, 10]
View
[10, 3, 2, 10, 1, 7, 10]
View
[4, 10, 3, 2, 10, 1, 7, 10]
View
[10, 4, 10, 3, 2, 10, 1, 7, 10]
View
[10, 7, 1, 10, 2, 3, 10, 4, 10]

Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

Python Class to Calculate Distance and Slope of a Line with Coordinates as Input

🐍  Can be run on Jupyter Notebook #CLASS DESIGNED TO CREATE OBJECTS THAT TAKES COORDINATES AND CALCULATES DISTANCE AND SLOPE class Line:     def __init__(self,coor1,coor2):         self.coor1=coor1         self.coor2=coor2 #FUNCTION CALCULATES DISTANCE     def distance(self):         return ((self.coor2[0]-self.coor1[0])**2+(self.coor2[1]-self.coor1[1])**2)**0.5 #FUNCTION CALCULATES SLOPE         def slope(self):         return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0]) #DEFINING COORDINATES coordinate1 = (3,2) coordinate2 = (8,10) #CREATING OBJECT OF LINE CLASS li = Line(coordinate1,coordinate2) #CALLING DISTANCE FUNCTION li.distance() #CALLING SLOPE FUNCTION li.slope()