title: Is there one variable way to create single linked list in python
body: <p>So I have the below single linked list class:</p>
  
  <pre><code>class ListNode:
      def __init__(self, x):
          self.val = x
          self.next = None
  </code></pre>
  
  <p>and now I create a single linked list from an array [-10,-3,0,5,9]</p>
  
  <pre><code>input = [-10,-3,0,5,9]
  head = ListNode(input[0])
  for idx, x in enumerate(input[1:]):
      if idx == 0:
          head.next = ListNode(x)
          temp = head.next
      else:
          temp.next = ListNode(x)
          temp = temp.next
  
  </code></pre>
  
  <p>Let's check my implementation:</p>
  
  <pre><code>while (head):
      print(head.val)
      head = head.next
  </code></pre>
  
  <p>the output is </p>
  
  <pre><code>-10
  -3
  0
  5
  9
  </code></pre>
  
  <p>now the output is correct. But as you saw, I used some ugly way of creating a single linked list instance, i.e., I used two variables: <code>temp</code> and <code>head</code>. Is there any way I can use only <code>head</code> or <code>temp</code>?</p>