题解 | #反转链表#

反转链表

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

两个链表解决:

public class Solution { public ListNode ReverseList(ListNode head) { if(head == null) return null; //定义新链表 ListNode reverseList = null;

    while(head != null){
        //保存下一节点和后面的所有节点
        ListNode node = head.next;
        //最关键的,从reverseList的头部插入
        head.next = reverseList;
        //reverseList更新
        reverseList = head;
        //赋值,将改变后的head只想燕来的下一个节点
        head = node;
    }

    return reverseList;
    
}

}

全部评论

相关推荐

自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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