Stack Data Structure In Java, patterns involved and Important problems on Leetcode.

Devendu
Javarevisited
Published in
3 min readAug 31, 2024

--

Photo by Daria Nepriakhina 🇺🇦 on Unsplash

I have solved over 30 + problems of Stack on Leetcode and I find stack is a very useful data structure that can be used in problems for better time complexity where brute force does not work

It involves the principle of FILO (First in Last out) or LIFO(Last in First Out). This means that the element that is inserted first will be accessible at the last and the one inserted at the last will be accessible first.

We will delve into multiple aspects of stack in this article which will help you understand Stack in general and its various aspects.

  1. Operations involved in Stack

We can insert an element to a stack, pop elements out of the stack and access elements from the top of the stack in O(1) operation which makes Stack a very good data structure when we want to improve O(n²) time complexity to O(n).

Insert :- push() is used to add an element to the top of the stack.

Stack<Integer> stack = new Stack();

stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);

Delete:- pop() is used to remove an element from the top of the stack.

stack.pop();
System.out.println(stack.peek());

Output:- 50

--

--

Devendu
Javarevisited

Software Engineering Associate @ Amdocs passionate about technology .I put stories about stuff I have learnt from my Hardwork and dedication