Hi all
I am having a problem with my method in my linked list class to split linked lists into two sublists.
The issue is that for some reason my first sublist has 3 more elements than the second sublist when I split a linked list with a total of 17 elements.
Here is the code for the method:
public void splitMid(LinkedListClass<T> sublist)
{
LinkedListNode<T> current;
if(count == 0)
{
System.out.println("List is Empty!");
}
else
{
current = first;
int subCount = count/2;
if(count%2 == 0)
{
for (int i = 0; i < subCount; i++)
{
current = current.link;
}
}
else
{
for (int i = 0; i < subCount + 1; i++)
{
current = current.link;
}
}
sublist.first = current.link;
sublist.last = last;
last = current;
last.link = null;
sublist.count = subCount;
}
}
Not entirely sure why it is not working but at least I got it so it actually splits the linked list LOL.