Python How to build a linked list in python

  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Build List Python
AI Thread Summary
The discussion focuses on building a linked list in Python with values 1, 2, and 3. The initial code provided has a critical error in the class initializer, using a single underscore instead of double underscores, which leads to an "object takes no parameters" error. Once corrected to `__init__`, the code functions properly, allowing the linked list to be created and the value of the root node to be printed. Participants highlight the importance of proper indentation in Python, noting that incorrect indentation can cause variables to be treated as part of the class definition rather than being executed in the main program. Additionally, suggestions are made to improve the structure of the code by creating separate classes for the linked list and its nodes, along with methods for adding nodes, to enhance clarity and functionality.
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
 
Technology news on Phys.org
You should rethink what classes you need and what operations should be done on the list (for instance to add a node).
 
You need two underscores. The initializer of a class should be __init__, not _init_.
 
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 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.
 
  • #10
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).
 

Similar threads

Replies
9
Views
3K
Replies
3
Views
1K
Replies
4
Views
2K
Replies
2
Views
969
Replies
23
Views
2K
Replies
2
Views
1K
Replies
15
Views
2K
Replies
16
Views
3K
Back
Top