您的位置:首页 > 编程语言 > Java开发

Java for LeetCode 206 Reverse Linked List

2015-06-07 19:34 483 查看
Reverse a singly linked list.

解题思路:

用Stack实现,JAVA实现如下:

public ListNode reverseList(ListNode head) {
if(head==null)
return null;
Stack<ListNode> stack =new Stack<ListNode>();
ListNode temp=head;
while(temp!=null){
stack.push(temp);
temp=temp.next;
}
head=stack.pop();
temp=head;
while(!stack.isEmpty()){
temp.next=stack.pop();
temp=temp.next;
}
temp.next=null;
return head;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: