Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Splitting circular list into two halves #529

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* The structure of linked list is the following
struct Node
{
int data;
struct Node *next;

Node(int x){
data = x;
next = NULL;
}
};
*/

// function which splits the circular linked list. head is pointer
// to head Node of given lined list. head1_ref1 and *head_ref2
// are pointers to head pointers of resultant two halves.

void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
if(head==NULL){
*head1_ref=NULL;
*head2_ref=NULL;
return;
}
if(head->next ==NULL){
*head1_ref=head;
*head2_ref=NULL;
return;
}
*head1_ref=head;
Node*current = head->next;
int total=1;
while(current!=head){
total++;
current = current -> next;
}
int n=(total+1)/2;
int m=total-n;
int count=1;
current=head;
while(count<n){
count++;
current=current->next;
}
*head2_ref=current->next;
current->next=head;
current =*head2_ref;
count=1;
while(count<m){
count++;
current=current->next;
}
current->next=*head2_ref;
}