题解 | #反转链表#

反转链表

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

/* public class ListNode { int val; ListNode next = null;

ListNode(int val) {
    this.val = val;
}

}*/ public class Solution { public ListNode ReverseList(ListNode head) { ListNode temp = null;//反转后的下一个节点head.next 初始值为空 相当于原最后的null ListNode next;//存放反转前的head.next if(head ==null){return null;} while(head!=null){ next = head.next;//反转之前把head.next存放在next中 防止指针断裂后找不到 head.next = temp;//把指针指向tenp temp = head;//由下一代码反推 head = next;//把要反转的节点往下移 } return temp; } }

全部评论

相关推荐

牛客76783384...:字节:不要放箭,活捉赵子龙
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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