You are viewing a single comment's thread. Return to all comments →
Slight variation which has the virtue of having fewer conditionals:
def InsertNth(head, data, position): if position == 0: return Node(data=data, next_node=head) else: curr_node = head for _ in range(position): prev_node = curr_node curr_node = curr_node.next prev_node.next = Node(data=data, next_node=curr_node) return head
Seems like cookies are disabled on this browser, please enable them to open this website
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →
Slight variation which has the virtue of having fewer conditionals: