Created
April 18, 2017 08:21
-
-
Save DeviaVir/c722e3f04ea6f3d6450ec7db17b7793d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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