forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path145.py
More file actions
41 lines (29 loc) · 716 Bytes
/
145.py
File metadata and controls
41 lines (29 loc) · 716 Bytes
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
36
37
38
39
40
41
"""
Problem:
Given the head of a singly linked list, swap every two nodes and return its head.
For example, given 1 -> 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3.
"""
from DataStructures.LinkedList import Node, LinkedList
def swap_nodes(ll: LinkedList) -> Node:
node1 = ll.head
node2 = ll.head.next
while True:
try:
node1.val, node2.val = node2.val, node1.val
node1, node2 = node1.next.next, node2.next.next
except:
break
return ll.head
if __name__ == "__main__":
LL = LinkedList()
LL.add(1)
LL.add(2)
LL.add(3)
LL.add(4)
print(LL)
print(swap_nodes(LL))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""