Saturday 28 November 2015

Traversing the Linked List (In Hindi) | लिंक सूची Traversing

लिंक सूची Traversing

हम ये मान लेते हैं की head पॉइंटर फर्स्ट नोड को पॉइंट कर रहा है। हमे traverse करने के लिए निम्नलिखित कदम लेने होंगे :-

  1. Follow the pointers.
  2. Display the contents of the nodes (or count) as they are traversed.
  3. Stop when the next pointer points to NULL.


listLength() फंक्शन लिंक्ड लिस्ट में कितने नोड्स हैं उन्हें बताता है।
यही फंक्शन लिंक्ड लिस्ट प्रिंट करने के लिए  भी  इस्सतमल में आ सकता है।

def listLength(self):
   current = self.head
   count = 0
   while current != None:
      count = count + 1
      current = current.getNext()
   return count

Time Complexity: O(n)
Space Complexity: O(1)

1 comment: