I'm pretty sure I'm on the right track with this, it's simply a method to split a linked list in half, setting it up for a merge sort. My intent is to split the list like this:
a->b->c->d->e
so that the new lists are a->c->e and b->d (I want it to actually return a pointer to the start of the second list, in this example, b)
Code:
Node *Node::split() {
if (next == NULL) return this; // if next is null, return this as list cannot be split further
Node *n = this->next; // create node that points to the node after this
this->next = n->next; // connects this to the node after this->next
n->next = n->next->split(); // calls split from the new node position
return n;
}
I need to get some other working code before I can test this method, so I just wanted to see if I was on the right track. Thoughts/suggestions are appreciated