divide a single linked list into 2 list even and odd list.
Hi all!
This is my first time posting to one of these things but here goes nothing.
I am having issues with one of my methods from my linked list class. It is just not splitting the list how it should.
Example: I enter 10 elements into this linked list single linked list then divide it into 2 list even and odd list and its not working.
the input is :
1 2 3 4 5 6 7 8 9 10
output :
evenlits
2 4 6 8 10
oddlist:
1 3 5 7 9
this is the code:
so i am thankfull for any help
Hi all!
This is my first time posting to one of these things but here goes nothing.
I am having issues with one of my methods from my linked list class. It is just not splitting the list how it should.
Example: I enter 10 elements into this linked list single linked list then divide it into 2 list even and odd list and its not working.
the input is :
1 2 3 4 5 6 7 8 9 10
output :
evenlits
2 4 6 8 10
oddlist:
1 3 5 7 9
this is the code:
public void splitMid(LinkedListClass<T> sublist)
{
LinkedListNode<T> evencurrent;
LinkedListNode<T> oddcurrent;
if(count == 0)
{
System.out.println("List is Empty!");
}
else
{
evencurrent = first;
oddcurrent=first;
if(count%2 == 0)
{
for (int i = 0; i < Count; i++)
{
evencurrent = evencurrent.link;
}
}
else
{
for (int i = 0; i < Count + 1; i++)
{
oddcurrent = oddcurrent.link;
}
}
evenlist.first = evencurrent.link;
evenlist.last = last;
last = even current;
last.link = null;
evenlist.count = Count;
}
}
so i am thankfull for any help