/*
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;//返回反转链表的头指针
}
}