码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 206 Reverse Linked List

时间:2015-06-07 20:15:41      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }

 

Java for LeetCode 206 Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4558946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!