On different sites, i was looking for programme in java doing reversing the order
of linked list(Singly Linked List and and doubly linked list) .I landed up on different sites like
1)http://geek-o-pedia.blogspot.com/2007/07/how-would-you-reverse-singly-linked.html
2)http://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively
PointA-As per my understanding these programmes(take link 1) are good when you are writing your linked list class as the
programme is assuming we can access Node class which we can not(As its is private inner class in Linked List.)
Point B-Apart from that this programme will permanently reverse the order of Source linked list. So when we iterate on this
we will always get the elemts in reverse order.
Please let me know if both the above points are correct
So i tried to do it myself
--Reversing the singly Linked List
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
LinkedList reverseList1 = new LinkedList();
int size= list1.size();
// below loop will revrse the order of source linked list i.e list1
for(int i =size-1;i>=0;i--)
{
reverseList1.add(size-i-1, list1.get(i));
}
-- For reversing the double linked list i.e linked list provided by jdk we can use reverse method of Collections class
Just wanted to ensure if both the approaches are correct as i could not find these approaches on internet.Everywhere i could find the approach similar to link1 and link2