Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Implement Stack using Linked List

Easy Acceptance 53.98% Points 20.00

You are provided with a linked list and need to implement the push and pop functionalities of a stack using this linked list. Your task is to complete the push() and pop() methods in the given class template. The push() method accepts an integer x as input and adds it to the stack, while the pop() method removes and returns the integer at the top of the stack. If the stack is empty, the pop() method should return -1.

Note: The input consists of a series of queries representing operations on the stack. There are two types of queries, as described below:

  1. Query type 1: This query includes an additional parameter x and performs the operation to push x into the stack.
  2. Query type 2: This query performs the operation to pop an element from the stack and return the popped value.
Examples
Example 1

Example 1

Input: [[1,2], [1,3], [2], [1,4], [2]]

Output: [3, 4]

Explanation:

Hints
Hint 1
Expected Time Complexity: O(1)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= numbers of calls made to push, pop <= 100
  • 1 <= x <= 100
Companies
Microsoft Samsung Cisco Cognizant
Topics
Stack
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions