题解 | #反转链表#
反转链表
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) {
//特殊值(空指针)处理
if(head == null){
return null;
}
ListNode prev = null;//当前指针的前向节点
ListNode next = head;//当前指针的后向节点
while(head != null){
next = head.next;//保存当前节点的后向节点的引用
head.next = prev;//更新当前指针的后向节点
prev = head;//更新当前指针的前向节点
head = next;//更新当前节点
}
return prev;//返回反转链表的头指针
}
}

查看9道真题和解析