How to build a linked list in python

  • Context: Python 
  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Build List Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 3K views
FallenApple
Messages
564
Reaction score
61

Homework Statement


[/B]
Build a linked list with values of 1, 2 and 3

The Attempt at a Solution



Python:
class ListNode(object):
    def _init_(self,x):
      self.val=x
      self.next=None
 
 
 
 
 
root=ListNode(1)  
root.next=ListNode(2)
root.next.next=ListNode(3)

print(root.val)

I keep getting error object takes no parameters
 
Physics news on Phys.org
QuantumQuest said:
You should rethink what classes you need and what operations should be done on the list (for instance to add a node).

I thought the root, root.next, and root.next.next connects the nodes while the right hand side of the equation declares them.
 
Dick said:
You need two underscores. The initializer of a class should be __init__, not _init_.
I tried that but I still get "error name root is not defined"
 
FallenApple said:
I tried that but I still get "error name root is not defined"

With the double underscores it works for me. It prints '1'. Can you repost the corrected code??
 
Dick said:
With the double underscores it works for me. It prints '1'. Can you repost the corrected code??

Python:
class ListNode(object):
    def __init__(self,x):
      self.val=x
      self.next=None
 
   
   
    root=ListNode(1)
    root.next=ListNode(2)
    root.next.next=ListNode(3)
   
   
   
   
print(root.val)
I ran it using https://repl.it/
 
FallenApple said:
Python:
class ListNode(object):
    def __init__(self,x):
      self.val=x
      self.next=None
 
  
  
    root=ListNode(1)
    root.next=ListNode(2)
    root.next.next=ListNode(3)
  
  
  
  
print(root.val)
I ran it using https://repl.it/

That's because the indentation levels on the lines using ListNode makes them part of the class definition. Indent them at the same level as the print statement. White spaces is syntax in Python.
 
  • Like
Likes   Reactions: FallenApple
Dick said:
That's because the indentation levels on the lines using ListNode makes them part of the class definition. Indent them at the same level as the print statement. White spaces is syntax in Python.
Got it! thanks. I'm used to using languages such as r or c++ where that isn't much of an issue because of curly braces.
 
FallenApple said:
I thought the root, root.next, and root.next.next connects the nodes while the right hand side of the equation declares them.

Be careful, this is an assignment - not an equation, that assigns the right hand side (return of call to class constructor) to the left hand side (node creation). What I meant in #2 is a general comment as I saw the code i.e. it would be better in my opinion to first create a separate class for linked list, another one for ListNode and you could add an operation like add_ListNode and then spot the error(s).