public static ListNode reverseNode(ListNode head)
{
Stack<ListNode> stack=new Stack<>();
ListNode temp=new ListNode();
ListNode res=new ListNode();
temp=head; //保存头结点
if(temp==null)
return null;
while (temp!=null) //注意循环条件,不是temp.next!=null
{
stack.push(temp);
temp=temp.next;
}
temp=stack.pop(); //先出栈一个作为新的头结点
res=temp;
while(!stack.empty()) //主语循环条件
{
temp.next=stack.pop();
temp=temp.next;
}
temp.next=null;
return res;
}
链表反转--栈实现
最新推荐文章于 2024-04-28 19:37:02 发布