Skip to content

Instantly share code, notes, and snippets.

@DeviaVir
Created April 18, 2017 08:21
Show Gist options
  • Save DeviaVir/c722e3f04ea6f3d6450ec7db17b7793d to your computer and use it in GitHub Desktop.
Save DeviaVir/c722e3f04ea6f3d6450ec7db17b7793d to your computer and use it in GitHub Desktop.
"""
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):
if not head or position == 0:
return Node(data, head)
position -= 1
head.next = InsertNth(head.next, data, position)
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment