题解 | #反转链表#

节点指针的使用,分三步:先定义好需要哪些节点指针,然后梳理节点指针的指向逻辑,最后更新指针节点,进入下一轮循环。

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode ReverseList (ListNode head) {
        // write code here
        //定义指针节点
        ListNode pre = null;
        ListNode cur = head;

        while(cur!=null){
            //提前保存下一节点
            ListNode temp = cur.next;
            //梳理指针节点的指向逻辑 
            cur.next = pre;
            //更新指针节点
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务