-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path25_reorder-list.cpp
91 lines (77 loc) · 1.87 KB
/
25_reorder-list.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// DATE: 02-Aug-2023
/* PROGRAM: 25_Linked List - Reorder List
https://leetcode.com/problems/reorder-list/
You are given the head of a singly linked-list. The list can be represented as:
L0 → L1 → … → Ln - 1 → Ln
Reorder the list to be on the following form:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2:
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
*/
// @ankitsamaddar @Aug_2023
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
void reorderList(ListNode *head) {
ListNode *slow = head;
ListNode *fast = head->next;
while (fast and fast->next) {
slow = slow->next;
fast = fast->next->next;
}
// reverse second half
ListNode *second = slow->next;
slow->next = NULL;
ListNode *prev = NULL;
while (second) {
ListNode *temp = second->next;
second->next = prev;
prev = second;
second = temp;
}
// merge alternately
ListNode *first = head;
second = prev;
while (second) {
ListNode *tmp1 = first->next;
ListNode *tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}
};
void printList(ListNode *head) {
while (head != NULL) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
int main() {
ListNode *list1 = new ListNode(1);
list1->next = new ListNode(2);
list1->next->next = new ListNode(3);
list1->next->next->next = new ListNode(4);
list1->next->next->next->next = new ListNode(5);
printList(list1);
Solution sol;
sol.reorderList(list1);
printList(list1);
return 0;
}