/*
题目描述:输入一个链表,反转链表后,输出新链表的表头。
题目链接:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pre=null;
ListNode next=null;
while(head!=null){
//临时存储:head的next节点信息
next=head.next;
//指针反转:head节点指向由next改为pre
head.next=pre;
//指针移动:pre和head依次向后移动
pre=head;
head=next;
}
return pre;
}
}