Linked-list Data Structure understanding
<p>I have a little difficulty in understanding one thing in the structure of the Linked-lists.
Basically nodes of a linked list are created using the following class, and the next reference is obtained by the method getNext():
I have omitted other methods as not relevant to my problem.</p>

<pre><code>class Node:
    def __init__(self,initdata):
       self.data = initdata
       self.next = None

    def getNext(self):
       return self.next
</code></pre>

<p>Now when creating a linkedlist and trying to find the size of the linked-list:</p>

<pre><code>class UnorderedList:
    def __init__(self):
        self.head = None

    def size(self):
        current = self.head
        count = 0
        while current != None:
            count = count + 1
            current = current.getNext()    &lt;-----

        return count
</code></pre>

<p>I do not understand the line shown with an arrow. I know the logic that it tries to traverse to the next node, but getNext() is the method of the "NodeClass". how is it (getNext() method) being used by an object (i.e. current) which is not a NodeClass object? and actually it is an object of the "UnorderedList" class.</p>

<p><code>current</code> is basically an instance of <code>UnOrderedList</code> in which each element is <code>Node</code> object. Hence the methods that are applied on <code>Nodes</code> can be applied on each element of <code>current</code>. Nodes are added to <code>UnOrderedList</code> using the add method.</p>

<pre><code>def add(self,item):
    temp = Node(item)
    temp.setNext(self.head)
    self.head = temp
</code></pre>

python class data-structures linked-list