Linked list की शुरुआत में एक node जोड़ना
पिछले अभ्यास में, आपने Node() और LinkedList() क्लास को इम्प्लिमेंट करना सीखा था.
इस अभ्यास में, आप insert_at_beginning() मेथड के लिए कोड तैयार करेंगे ताकि लिंक्ड लिस्ट की शुरुआत में एक नया नोड जोड़ा जा सके.
Node() क्लास को याद करें:
class Node:
def __init__(self, data):
self.data = data
self.next = None
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Data Structures और Algorithms
अभ्यास निर्देश
- नया नोड बनाएँ.
- जाँचें कि लिंक्ड लिस्ट में
headनोड है या नहीं. - अगर लिंक्ड लिस्ट में
headनोड है, तो नए नोड केnextकोheadकी ओर पॉइंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def insert_at_beginning(self, data):
# Create the new node
new_node = ____(data)
# Check whether the linked list has a head node
if self.____:
# Point the next node of the new node to the head
new_node.___ = self.____
self.head = new_node
else:
self.tail = new_node
self.head = new_node