-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_63.cpp
More file actions
44 lines (32 loc) · 1.22 KB
/
Day_63.cpp
File metadata and controls
44 lines (32 loc) · 1.22 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
/*
DAY 63 : Split a Circular Linked List into two halves.
https://www.geeksforgeeks.org/split-a-circular-linked-list-into-two-halves/
QUESTION : Given a Cirular Linked List of size N, split it into two halves circular lists.
If there are odd number of nodes in the given circular linked list then out of the resulting
two halved lists, first list should have one node more than the second list. The resultant
lists should also be circular lists and not linear lists.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
0 <= N <= 100
*/
void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
// your code goes here
Node* double_ptr = head;
Node* single_ptr = head;
if(head == NULL)
return;
while(double_ptr->next != head && double_ptr->next->next != head) {
double_ptr = double_ptr->next->next;
single_ptr = single_ptr->next;
}
if(double_ptr->next->next == head)
double_ptr = double_ptr->next;
*head1_ref = head;
if (head->next!= head) {
*head2_ref = single_ptr->next;
}
double_ptr->next = single_ptr->next;
single_ptr->next = head;
}