-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ24SwapNodesInPairs.java
More file actions
30 lines (26 loc) · 924 Bytes
/
Q24SwapNodesInPairs.java
File metadata and controls
30 lines (26 loc) · 924 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
/*
@b-knd (jingru) on 02 August 2022 10:17:00
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
//set head of the list to be the second node (this will be returned in the end)
ListNode dummyHead = head.next;
ListNode curr = head;
ListNode prev = new ListNode();
//change next link in while loop
while(curr != null && curr.next != null){
ListNode temp = curr.next;
prev.next = temp;
curr.next = curr.next.next;
temp.next = curr;
prev = curr;
curr = curr.next;
}
return dummyHead;
}
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Swap Nodes in Pairs.
//Memory Usage: 42.6 MB, less than 5.73% of Java online submissions for Swap Nodes in Pairs.