[
linked list
]
BinarySearch 1020 Insert Into Linked List
Problem statement
https://binarysearch.com/problems/Insert-Into-Linked-List/
Solution
Just do what is asked, add dummy variable in the beginning.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, head, pos, val):
dummy = LLNode(-1)
dummy.next = head
curr = dummy
for _ in range(pos):
curr = curr.next
nxt = curr.next
curr.next = LLNode(val)
curr.next.next = nxt
return dummy.next