Geeks For Geeks Reverse A Linked List
class Solution
{
//Function to reverse a linked list.
Node reverseList(Node head)
{
Stack<Integer> stack=new Stack<Integer>();
Node temp=head;
while(temp!=null){
stack.push(temp.data);
temp=temp.next;
}
temp=head;
while(!stack.isEmpty()){
temp.data=stack.pop();
//System.out.println("Data is: "+temp.data);
temp=temp.next;
}// code here
return head;
}
}
Comments
Post a Comment