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

Popular posts from this blog

Java Date And Time HackerRank

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024

Minimum Indices HackerEarth