← 返回 bytedance 的题目列表Implement Stack Using Linked List
类型:online_judge
Implement a stack using a linked list. The stack should support the following functions:
push(val): Add an element to the top of the stack.
pop(): Remove the top element from the stack and return its value.
peek(): Return the value of the top element without removing it.
isEmpty(): Check if the stack is empty.
Requirements:
All operations should have O(1) time complexity.
Use a linked list to store the data.
Test Cases:
Input:
push(1)
push(2)
peek()
pop()
isEmpty()
Output:
2
2
False
Example
Input
push(1)
push(2)
peek()
pop()
isEmpty()