title: How to get the head node in linkedlist python
body: <p>I've been trying to find a way to get the first data.</p>
  
  <p>this is my data in database for example.</p>
  
  <pre><code>id    name     next
  001   task1    002
  002   task2    003
  003   task3    000
  </code></pre>
  
  <p>what I want to do is to get the first value of the headnode which should be <code>001</code> as shown in the database.</p>
  
  <p>this is my code</p>
  
  <pre><code>while True:
      self.headNode = 000
      try:
          lastNode = Task.objects.get(next=self.headNode)
          self.headNode = lastNode.id
          break
      except Task.DoesNotExist:
          break
  </code></pre>
  
  <p>What I expect here, the value of self.headNode should be <code>001</code> but it gave me <code>003</code>. Please help me how to get that <code>001</code>. I've been working on this for almost a month now. </p>
  
  <p>this is the other data example.</p>
  
  <pre><code>id    name     next
  222   task1    444
  333   task2    222
  444   task3    000
  </code></pre>
  
  <p>in here... the head node should be <code>333</code> and to know that. i will just get the data where the <code>next</code> is <code>000</code>. Then get its so it is <code>444</code>. then I will get the other data where the next is <code>444</code> and the <code>id</code> is <code>222</code>. lastly get data were <code>next</code> is <code>222</code> then you will get the id <code>333</code>. so that would show that the head node is <code>333</code>. I hope I have explained it clearly.  </p>