/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.util.Stack;
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null){
return null;
}
Stack<ListNode> s = new Stack<ListNode>();
while(head!=null){
s.push(head);
head=head.next;
}
ListNode res = s.pop();
ListNode temp = res;
while(!s.isEmpty()){
temp.next = s.pop();
temp=temp.next;
}
temp.next=null;
return res;
}
}
利用栈来反转链表
最新推荐文章于 2024-03-11 16:04:42 发布