-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorder List.cpp
More file actions
126 lines (111 loc) · 3.6 KB
/
Reorder List.cpp
File metadata and controls
126 lines (111 loc) · 3.6 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Definition for singly-linked list.
* 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) {
//head2 = new list, head - pointer to the linkedlist. temp2 - pointer to the new list
ListNode *head2 = NULL, *temp = head, *temp2;
//if only one node, return it self
if(head->next == NULL)
return;
//length - length of the linked list
int length = 0, counter = 0;
//calculate the length of the list
while(temp!=NULL)
{
temp=temp->next;
length++;
}
temp = head;
int half_length = (length % 2 == 0 ? length/2: length/2 + 1);
//copy only second half of linked list into the new list
while(counter < length)
{
if(counter >= half_length)
{
if(head2==NULL)
{
head2 = new ListNode;
head2->val = temp->val;
head2->next = NULL;
temp2 = head2;
}
else
{
temp2->next = new ListNode;
temp2 = temp2->next;
temp2->val = temp->val;
temp2->next = NULL;
}
}
temp=temp->next;
counter++;
}
//reverse second list
// pointers that traverse through the linked list
ListNode *current=head2, *next=NULL, *prev=NULL;
//iterate through the list to reverse it
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current = next;
}
//assign head to the last element
head2 = prev;
temp = head;
temp2 = head2;
//iterate throguh both lists and insert each nodes from those lists into the new list alternatively until the second list(reversed half of list 1) is traversed completely.
ListNode *result = NULL, *temp3;
while(temp2 != NULL)
{
if(result == NULL)
{
result = new ListNode;
result->val = temp->val;
result->next = new ListNode;
result->next->val = temp2->val;
result->next->next = NULL;
temp3 = result->next;
}
else
{
temp3->next = new ListNode;
temp3 = temp3->next;
temp3->val = temp->val;
temp3->next = new ListNode;
temp3 = temp3->next;
temp3->val = temp2->val;
temp3->next = NULL;
}
temp = temp->next;
temp2 = temp2->next;
}
//if length is odd , then add the remaining node into the new list
if(length % 2 != 0)
{
temp3->next = new ListNode;
temp3 = temp3->next;
temp3->val = temp->val;
temp3->next = NULL;
}
//copy each values of the new list into the first list
temp3 = result;
temp = head;
while(temp3!=NULL)
{
temp->val = temp3->val;
temp = temp->next;
temp3 = temp3->next;
}
}
};