Skip to main content

Find the smallest number in the doubly linked list in python

Python program for Find the smallest number in the doubly linked list. Here problem description and explanation.

#  Python 3 program for
#  Find the smallest node in doubly linked list

#  Linked List Node
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
		self.prev = None
	

class DoublyLinkedList :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Insert new node at end position
	def insert(self, value) :
		#  Create a node
		node = LinkNode(value)
		if (self.head == None) :
			#  Add first node
			self.head = node
			self.tail = node
			return
		
		#  Add node at last position
		self.tail.next = node
		node.prev = self.tail
		#  new last node
		self.tail = node
	
	#  Find smallest number
	def minNumber(self) :
		if (self.head == None) :
			print("Empty Linked List")
		else :
			#  Get first node of linked list
			temp = self.head
			result = temp.data
			#  iterate linked list 
			while (temp != None) :
				if (temp.data < result) :
					#  Get new minimum
					result = temp.data
				
				#  Visit to next node
				temp = temp.next
			
			print("Smallest : ", result)
		
	

def main() :
	dll = DoublyLinkedList()
	#  Insert following linked list nodes
	dll.insert(14)
	dll.insert(31)
	dll.insert(12)
	dll.insert(15)
	dll.insert(11)
	dll.insert(25)
	dll.minNumber()

if __name__ == "__main__": main()

Output

Smallest :  11




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment