反转链表

反转链表

http://www.nowcoder.com/questionTerminal/75e878df47f24fdc9dc3e400ec6058ca

三个变量就够用了
pre cur next

    public ListNode ReverseList3(ListNode head) {
        ListNode pre = null;
        ListNode next = null;

        while (head != null) {
            // 记录next 反转后会丢失
            next = head.next;
            // 反转 链表一分为二了
            head.next = pre;
            // 前端链表的头部
            pre = head;
            // 后端链表的头部
            head = next;
        }
        return pre;
    }
全部评论

相关推荐

点赞 评论 收藏
分享
洒脱的馒头:校友看我帖子,这种不面试或者面试简单的,都是进去当牛马的。更差的可能一两周,觉得你代码能力不行就把你开除,反正他招人成本低
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务