您的位置:首页 > 其它

206. Reverse Linked List

2016-02-10 23:46 323 查看
用递归或者栈

递归

public class Solution {

    public ListNode reverseList(ListNode head) {

        if(head==null || head.next==null)

            return head;

        ListNode nextNode=head.next;

        ListNode newHead=reverseList(nextNode);

        nextNode.next=head;

        //head.next=null;

        return newHead;

    }

}



public class Solution {

    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;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: