This question is a hackerrank challenge. link here: https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list
"""
Insert Node at a specific position in a linked list
head input could be None as well for empty list
Node is defined as
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
return back the head of the linked list in the below method.
"""
#This is a "method-only" submission.
#You only need to complete this method.
def InsertNth(head, data, position):
node = head
if position == 0:
node = Node(data)
node.data = data
node.next = head
return node
else:
while position > 0:
node = node.next
i = node.next
node.next = Node(data)
node.next.next = i
return head
my current output is 321024, but I need it to be 310542. Any help is greatly appreciated!
whileloop doesn't make any sense; it's going to always run exactly one time.