forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest50.java
More file actions
41 lines (40 loc) · 864 Bytes
/
test50.java
File metadata and controls
41 lines (40 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package byteDance;
/**
* @Author:lizeyang
* @Date:2020/5/23 10:49
* function:链表实现一个栈
*/
public class test50 {
static ListNode top;
public static void put(int data){
if(top==null){
top = new ListNode(data);
}else{
//头插法
ListNode node = new ListNode(data);
node.next = top;
top= node;
}
}
public static int get(){
int res = 0;
if(top!=null){
res = top.val;
top = top.next;
}
return res;
}
public static int peek(){
int res = 0;
if(top!=null){
res = top.val;
}
return res;
}
public static void main(String[] args) {
put(1);
put(2);
System.out.println(peek());
System.out.println(get());
}
}