Question: Reverse the contents of a stack using only stack operations [ push() and pop() ]. public class LinkedList { Node head; // head of list

Reverse the contents of a stack using only stack operations [ push() and pop() ].

public class LinkedList

{

Node head; // head of list

//Linked list Node

class Node

{

int data;

Node next;

Node(int d) {data = d; next = null; }

}

// Inserts a new Node at front of the list.

public void push(int new_data)

{

// 1 & 2: Allocate the Node & Put in the data

Node new_node = new Node(new_data);

// 3. Make next of new Node as head

new_node.next = head;

// 4. Move the head to point to new Node

head = new_node;

}

int detectLoop()

{

Node slow_p = head, fast_p = head;

while (slow_p != null && fast_p != null && fast_p.next != null) {

slow_p = slow_p.next;

fast_p = fast_p.next.next;

if (slow_p == fast_p) {

System.out.println("Found loop");

return 1;

}

}

return 0;

}

// Drier program to test above functions

public static void main(String args[])

{

LinkedList llist = new LinkedList();

llist.push(20);

llist.push(4);

llist.push(15);

llist.push(10);

//Create loop for testing

llist.head.next.next.next.next = llist.head;

llist.detectLoop();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!