-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathReverseAlternateKNodes.cpp
More file actions
58 lines (48 loc) · 1.05 KB
/
Copy pathReverseAlternateKNodes.cpp
File metadata and controls
58 lines (48 loc) · 1.05 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*Given a linked list A of length N and an integer B.
You need to reverse every alternate B nodes in the linked list A.
LINK - https://www.interviewbit.com/problems/reverse-alternate-k-nodes/
Example Input
Input 1:
A = 3 -> 4 -> 7 -> 5 -> 6 -> 6 -> 15 -> 61 -> 16
B = 3
Input 2:
A = 1 -> 4 -> 6 -> 6 -> 4 -> 10
B = 2
Example Output
Output 1:
7 -> 4 -> 3 -> 5 -> 6 -> 6 -> 16 -> 61 -> 15
Output 2:
4 -> 1 -> 6 -> 6 -> 10 -> 4
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::solve(ListNode* A, int B) {
if (A == NULL || A->next == NULL) {
return A;
}
ListNode* tail = A, *head;
ListNode* curr = A, *prev, *nxt;
for (int i = 0; i < B; i++) {
nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
}
head = prev;
if (curr) {
tail->next = curr;
for (int i = 0; i < B; i++) {
tail = curr;
curr = curr->next;
}
}
ListNode* temp_head = solve(curr, B);
tail->next = temp_head;
return head;
}