class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40) head = insert_at_position(head, 25, 3) print(print_list(head))
The new node with value 25 is inserted after the second node (20), so it becomes the third node. The list updates to 10 -> 20 -> 25 -> 30 -> 40 -> null.
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(5) head.next = Node(15) head.next.next = Node(25) head = insert_at_position(head, 1, 1) print(print_list(head))
The new node with value 1 is inserted at the start, so it points to the old head (5). The list becomes 1 -> 5 -> 15 -> 25 -> null.
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) current = head count = 1 while current and count < pos: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head head = Node(10) head.next = Node(20) head.next.next = Node(30) head = insert_at_position(head, 50, 4) # Print list result = [] cur = head while cur: result.append(cur.data) cur = cur.next print(result)
The loop moves current to the node at position 4, but insertion requires stopping at position 3 to link the new node. Changing the loop to count < pos - 1 fixes this.
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_position(head, data, pos): new_node = Node(data) if pos == 1: new_node.next = head return new_node current = head count = 1 while current and count < pos - 1: current = current.next count += 1 if not current: return head new_node.next = current.next current.next = new_node return head def print_list(head): result = [] current = head while current: result.append(current.data) current = current.next return result head = Node(1) head.next = Node(2) head.next.next = Node(3) head = insert_at_position(head, 99, 5) print(print_list(head))
The function returns the original list because position 5 is beyond the current list length of 3, so no insertion happens.
To insert at position k, you must move through k-1 nodes to reach the insertion point, so the time is proportional to k, which is O(k).
