How to build a linked list in python

In summary: With the double underscores it works for me. It prints '1'. Can you repost the corrected code??With the double underscores it works for me. It prints '1'. Can you repost the corrected code??
  • #1
FallenApple
566
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
  • #2
You should rethink what classes you need and what operations should be done on the list (for instance to add a node).
 
  • #3
You need two underscores. The initializer of a class should be __init__, not _init_.
 
  • #4
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.
 
  • #5
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"
 
  • #6
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??
 
  • #7
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/
 
  • #8
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
  • #9
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).
 

1. What is a linked list in Python?

A linked list is a data structure in Python that is used to store a collection of items. Each item, known as a node, contains a value and a pointer to the next node in the list. This allows for efficient insertion and deletion of items in the list.

2. How do you create a linked list in Python?

To create a linked list in Python, you can define a class that represents a node in the list. The class should have attributes for the value of the node and a pointer to the next node. Then, you can create instances of the class and link them together to form the list.

3. How do you insert an item into a linked list in Python?

To insert an item into a linked list in Python, you first need to find the appropriate location in the list where the item should be inserted. Then, you can create a new node with the item's value and link it to the appropriate nodes in the list. Finally, you can update the pointers to ensure the list remains connected.

4. How do you delete an item from a linked list in Python?

To delete an item from a linked list in Python, you first need to find the node containing the item. Then, you can update the pointers to bypass the node and remove it from the list. If the node is the head or tail of the list, special cases must be considered to maintain the integrity of the list.

5. What are the advantages of using a linked list in Python?

Some advantages of using a linked list in Python include:

  • Efficient insertion and deletion of items
  • Dynamic size - the list can grow or shrink as needed
  • Easy to implement and understand
  • Flexibility - nodes can be easily rearranged or removed

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
649
  • Programming and Computer Science
Replies
7
Views
429
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
992
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
767
Back
Top