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.

683 K Empty Slots

  Approach #1: Insert Into Sorted Structure [Accepted] Intuition Let's add flowers in the order they bloom. When each flower blooms, we check it's neighbors to see if they can satisfy the condition with the current flower. Algorithm We'll maintain  active , a sorted data structure containing every flower that has currently bloomed. When we add a flower to  active , we should check it's lower and higher neighbors. If some neighbor satisfies the condition, we know the condition occurred first on this day. Complexity Analysis Time Complexity (Java):  O(N \log N) O ( N lo g N ) , where  N N  is the length of  flowers . Every insertion and search is  O(\log N) O ( lo g N ) . Time Complexity (Python):  O(N^2) O ( N 2 ) . As above, except  list.insert  is  O(N) O ( N ) . Space Complexity:  O(N) O ( N ) , the size of  active . Approach #2: Min Queue [Accepted] Intuition For each contiguous block ("window") of  k  po...