-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ1721SwappingNodes.java
36 lines (30 loc) · 1.04 KB
/
Q1721SwappingNodes.java
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
/*
@b-knd (jingru) on 01 August 2022 17:28:00
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
ListNode frontNode = head, rearNode = head, temp = head;
int size = 0;
//use first while loop to find frontNode to be swapped and size of the linked list;
while(temp != null){
size++;
if(size < k){
frontNode = frontNode.next;
}
temp = temp.next;
}
//use second while loop to find rearNode to be swapped
int count = 1;
while(count < size-k+1){
rearNode = rearNode.next;
count++;
}
//swap node values
int temp2 = frontNode.val;
frontNode.val = rearNode.val;
rearNode.val = temp2;
return head;
}
}
//Runtime: 3 ms, faster than 86.82% of Java online submissions for Swapping Nodes in a Linked List.
//Memory Usage: 178.7 MB, less than 62.19% of Java online submissions for Swapping Nodes in a Linked List.