1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to build a linked list in python

  1. Jan 30, 2017 #1
    1. The problem statement, all variables and given/known data

    Build a linked list with values of 1, 2 and 3




    3. The attempt at a solution

    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 keep getting error object takes no parameters
     
  2. jcsd
  3. Jan 30, 2017 #2

    QuantumQuest

    User Avatar
    Gold Member

    You should rethink what classes you need and what operations should be done on the list (for instance to add a node).
     
  4. Jan 30, 2017 #3

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    You need two underscores. The initializer of a class should be __init__, not _init_.
     
  5. Jan 30, 2017 #4
    I thought the root, root.next, and root.next.next connects the nodes while the right hand side of the equation declares them.
     
  6. Jan 30, 2017 #5
    I tried that but I still get "error name root is not defined"
     
  7. Jan 30, 2017 #6

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    With the double underscores it works for me. It prints '1'. Can you repost the corrected code??
     
  8. Jan 30, 2017 #7
    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/
     
  9. Jan 30, 2017 #8

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  10. Jan 30, 2017 #9
    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.
     
  11. Jan 30, 2017 #10

    QuantumQuest

    User Avatar
    Gold Member

    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).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to build a linked list in python
Loading...