1
# Runtime: 784 ms (Top 40.9%) | Memory: 50.79 MB (Top 42.2%)5
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:6
tot = 0 # initialise total8
while Head: # count total nodes9
Head = Head.next # move forward10
tot += 1 # incerse count by one for each node11
one, two = None, None # two pointers of one and two13
1 # we're initialising to one because we have one based index for swapping15
Head = head # regain original head to traverse18
break # if we have both one and two then break loop to save time21
): # if from forward we reach at node k then it's our first node25
): # if from backward we reach to node k then save it27
Head = Head.next # move further28
count += 1 # increse count29
one.val, two.val = two.val, one.val # now swap values30
return head # return head