首页 > 其他 > 详细

[LeetCode] Reverse Linked List

时间:2015-07-13 15:29:22      阅读:230      评论:0      收藏:0      [点我收藏+]

Reverse a singly linked list.

思路:新建一个ListNode,最后返回这个ListNode.next。中间使用两个临时指针一个指向原来的链表头,一个指向新的链表的next.每次循环都往里面插入新的tempNode.

时间复杂度:O(n)

代码:

    public ListNode reverseList(ListNode head) {
        ListNode node=new ListNode(0);
        ListNode tempNode=null;
        ListNode tempHead=head;
        while(head!=null)
        {
            tempHead=head;
            head=head.next;
            node.next=tempHead;
            tempHead.next=tempNode;
            tempNode=node.next;
        }
        return node.next;
    }

优化:

扩展:

[LeetCode] Reverse Linked List

原文:http://www.cnblogs.com/maydow/p/4642666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!