-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ24SwapNodesInPairs.py
More file actions
32 lines (23 loc) · 949 Bytes
/
Q24SwapNodesInPairs.py
File metadata and controls
32 lines (23 loc) · 949 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
# @b-knd (jingru) on 07 August 2022 16:11:00
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None or head.next == None:
return head
dummyHead = head.next
curr = head
prev = ListNode(0)
while curr != None and curr.next != None:
prev.next = curr.next
temp = curr.next
curr.next = curr.next.next
temp.next = curr
prev = curr
curr = curr.next
return dummyHead
#Runtime: 36 ms, faster than 84.86% of Python3 online submissions for Swap Nodes in Pairs.
#Memory Usage: 14 MB, less than 18.57% of Python3 online submissions for Swap Nodes in Pairs.