Note:This is not a question. I jst like to share some methods in java which I feel will be helpful. It seems they are most commonly needed ones.:)
The method to reverse elements in linked list public void reverse() { Link current=first; Link forward; Link previous = null; while(current!=null) { forward=current.next; current.next = previous; previous=current; current=forward; } first=previous; } The method to find the middle element in a linked list public Link midelement() { Link current = first; Link middle =first; int length = 0; while(current!=null) { length++; if(middle != current && length%2 != 0) { middle=middle.next; } current=current.next; } return middle; } The method to find the size of the linked list public int size() { Link current=first; int length=0; while(current!=null) { length++; current=current.next; } return length; } The method to find the nth element in a linked list public Link nthElement(int n) { Link current = first; for(int length = 1;length< n;length++) { current = current.next; } return current; }
The method for reversing elements that I posted here is non recursive method
I havent studied java yet put the code looks interesting :)
http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html A lot of these already exist in the API. The API is your most powerful tool.
if you're using python, list.reverse() exists as a function.
you would better share whole class, but yeah much things already exists in API however its good to write it yourself for practise or even better do that in C >:) have fun with pointers
thnx:)
Join our real-time social learning platform and learn together with your friends!