-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswapping-nodes-in-a-linked-list.py
More file actions
35 lines (24 loc) · 1.02 KB
/
swapping-nodes-in-a-linked-list.py
File metadata and controls
35 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""
Initial thoughts:
We move a first pointer k positions forward and save a reference to the node.
Then we create another pointer at head and move it forward in sync with the first pointer while the first pointer hasn't hit the end of the linked list. When the first pointer hits the end, the second pointer is at the second node that needs to be swapped. Then we swap.
Time complexity: O(n)
Space complexity: O(1)
"""
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
first = dummy_head = ListNode(0, head)
for _ in range(k):
first = first.next
left_node = first
second = dummy_head
while first:
first, second = first.next, second.next
left_node.val, second.val = second.val, left_node.val
return head