题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

 private static ListNode rev(ListNode head){
        ListNode  pre = null;
        ListNode cur = head;
        while (cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

主要思路是:pre作为新的链表存储数据,cur是指向当前链表数据
2.链表反转,指针方向发生改变,由尾部节点指向null,改为头部节点指向null,内部其他指针方向都改变
3.cur.next = pre;第一个节点指向null值
4. pre = cur;  头节点后移动
5. cur = temp;  temp是保存未处理的数据,赋值给cur

全部评论

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务