JAVA
Implement a θ(n) time non-recursive program that reverses a singly linked list L of n elements. The program gets the head of the list (L.head) as input and cannot use any storage other than the given list.
Don't not use recursive~~~~~~~
thanks
Node reverseList(Node temp)
{
Node previous = null; //this will get previous value
Node cursor = temp; // this will point to the current node
Node next = null; // this will get next position
while (cursor != null) { // non-recursive implementation
//until our cursor reaches null
next = cursor.next; // get next node which is cursor next
cursor.next = previous; //now update cursor next to previous because of reverse traversal
previous = cursor;
cursor = next;
}
temp = previous;
return temp;
}
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP
Get Answers For Free
Most questions answered within 1 hours.