1
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def __len__(self):
        cur = self.head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def append(self, item):
        cur = self.head
        while cur is not None:
            cur = cur.next
        cur.next = ?

I am trying to append to the linkedlist but I cant use 'cur.next' as cur has no attribute 'next'. Any hints on this?

Thanks!

My test cases:

def test_append_empty() -> None:
    lst = LinkedList()
    lst.append(1)
    assert lst.head.data == 1


def test_append_one() -> None:
    lst = LinkedList()
    lst.head = Node(1)
    lst.append(2)
    assert lst.head.next.data == 2

2 Answers2

0

You have to look for the case when self.head is None as Nonetype will have no attribute next. Also iterate till you find the next pointer in the node to be None. So it should be cur.next is not None rather than cur is None which is finding if the Node is None. Logically speaking you cannot append anything to None

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def __len__(self):
        if not self.head:
            return 0
        cur = self.head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def append(self, item):
        if not self.head:
            self.head=Node(item)
            return
        cur = self.head
        while cur.next is not None:
            cur = cur.next
        cur.next = Node(item)

Test case1

l=LinkedList()
l.append(1)
l.__len__() #prints 1

Test case2

l=LinkedList()
l.append(2)
l.append(3)
l.__len__() #2

temp=l.head
while(temp):
    print(temp.data)
    temp=temp.next

Including testcases of OP as well

def test_append_empty():
    lst = LinkedList()
    lst.append(1)
    print(lst.head.data == 1)


def test_append_one():
    lst = LinkedList()
    lst.head = Node(1)
    lst.append(2)
    print(lst.head.next.data == 2)

test_append_empty() #True
test_append_one() #True
mad_
  • 8,121
  • 2
  • 25
  • 40
  • Dont you think there will be an attribute error on the last line? –  Oct 08 '18 at 16:42
  • I just have. I got a AttributeError! –  Oct 08 '18 at 16:47
  • @Kaisr it is hard to believe that. I might have edited my answer since you had executed it. As I have run the testcases and it works just fine for me. If not please include the full stacktrace – mad_ Oct 08 '18 at 16:49
  • can i send you the whole code without edits via email. I have my own testcases too –  Oct 08 '18 at 16:51
  • No, please post here. Will be beneficial for others as well. As I cannot see any errors executing the above code. – mad_ Oct 08 '18 at 16:53
  • The len() member is not correct, len(empty_list) should return 0, not -1 – Axel Paccalin Oct 08 '18 at 17:35
0

I hope that with the clear example of mad_ you have understood the idea that you still have this concept of nodes to handle.

A linked list not only is a list of values, but also a list of links.

Since you seemed interested to do it in one class, here is a quick implementation:

class LinkedList:
    def __init__(self, item=None):
        self.next = None
        self.val = item

    def __len__(self):
        cur = self
        count = 1 if self.val is not None else 0
        while cur.next is not None:
            count += 1
            cur = cur.next
        return count    

    def append(self, item):
        if not self.val:
            self.val = item
            return

        cur = self
        while cur.next is not None:
            cur = cur.next
        cur.next = LinkedList(item)

Edit:

Since you've included mad_'s len() member to your question, I also added the one adapted for this class.

And here are some usage examples:

myList = LinkedList()
myList.append('a')
myList.append('b')
myList.append('c')
myList.append('d')
print(len(myList))
Axel Paccalin
  • 136
  • 1
  • 13
  • I dont have a self.val in my LinkedList class so I cant relate :( –  Oct 08 '18 at 17:43
  • The val is actually the value of each node: myList.val == 'a', myList.next.val == 'b' ... Wether it is by having a seperate node class or by having it directly in the LinkedList class, you need a val member, you can call it data or item or whatever you like! – Axel Paccalin Oct 08 '18 at 18:30
  • nvm I fixed it without changing class documentation –  Oct 08 '18 at 19:31