-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1721.c
37 lines (30 loc) · 773 Bytes
/
m1721.c
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
/* Took 3:05 total -- first attempt was in C and was quick despite that hehe
* 97.79% runtime
* 95.59% memory :D
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapNodes(struct ListNode* head, int k) {
struct ListNode* left = head;
for (int i = 1; i < k; i++) {
left = left->next;
}
struct ListNode* rightSlow = head;
struct ListNode* rightFast = head;
for (int i = 0; i < k; i++) {
rightFast = rightFast->next;
}
while (rightFast) {
rightFast = rightFast->next;
rightSlow = rightSlow->next;
}
int tempVal = left->val;
left->val = rightSlow->val;
rightSlow->val = tempVal;
return head;
}