Sidharth Samant - 10 months ago 47

Python Question

There's this HackerRank problem about implementing a linked list. It is pretty basic and I thought I'd do it in a jiffy since I'd done all kinds of linked list implementations in C++. But I'm stuck somewhere.

`class Node:`

def __init__(self,data):

self.data = data

self.next = None

class Solution:

def display(self,head):

current = head

while current:

print current.data,

current = current.next

def insert(self,head,data):

new_node = Node(data)

if head == None:

head = new_node

else:

current = head

while current.next:

current = current.next

current.next = new_node

mylist= Solution()

T=int(input())

head=None

for i in range(T):

data=int(input())

head=mylist.insert(head,data)

mylist.display(head)

Only the

`insert`

`head`

Answer

You are appending the `new_node`

after all the elements, and also you are *not* returning the newly created node. Modify your `insert`

method like this:

```
def insert(self,head,data):
new_node = Node(data)
if head is not None:
current = head
new_node.next = current
return new_node
```