In python, Give an algorithm for finding the second-to-last node in a singly linked
list in which the last node is indicated by a next reference of None.
class Node:
def __init__(self, data):
self.data = data
self.next = None
n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5)
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
temp = n1
prev = None
while temp.next != None:
prev = temp
temp = temp.next
print(prev.data)
Code :
Output :
Get Answers For Free
Most questions answered within 1 hours.