How to build a linked list in python

  • Context: Python 
  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Build List Python
Click For Summary

Discussion Overview

The discussion revolves around building a linked list in Python, specifically addressing a homework problem that requires creating a linked list with values of 1, 2, and 3. Participants explore coding errors, class structure, and potential improvements in the implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant reports an error stating "object takes no parameters" due to an incorrect class initializer.
  • Another participant suggests rethinking the class structure and operations needed for the linked list, such as adding nodes.
  • Several participants emphasize the need for two underscores in the class initializer, correcting the original code.
  • A participant mentions encountering a "name root is not defined" error after attempting corrections.
  • One participant successfully runs the corrected code and confirms it prints '1', asking for the corrected version to be reposted.
  • Another participant points out that indentation issues in Python can lead to parts of the code being incorrectly interpreted as part of the class definition.
  • A participant reflects on their experience with other programming languages, noting differences in syntax regarding indentation.
  • One participant suggests creating a separate class for the linked list and another for ListNode, proposing the addition of operations like adding nodes.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct syntax and class structure, but there are differing opinions on the best approach to structuring the linked list and its operations. The discussion remains unresolved regarding the optimal design of the linked list implementation.

Contextual Notes

There are limitations related to assumptions about class design and the handling of Python syntax, particularly regarding indentation and the use of underscores in method names.

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   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.
 
  • #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 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K