题解 | #反转链表#

反转链表

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

1.定义两个指针pre、next;链表为pre-head-next-【..】
2.遍历链表,调整指针指向完成反转
next = head.next;
head.next = pre;//反转
pre = head;
head = next;
----------java代码------------
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
//pre head next
ListNode pre = null;//指向前一个节点
ListNode next = null;
while(head != null){
next = head.next;
head.next = pre;//完成链表反转
pre = head;
head = next;
}
return pre;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务