public class Solution {
public ListNode ReverseList(ListNode head) {
if (head==null){
//判断为null的情况
return null;
}
ListNode pre=null;
ListNode next=null;
while (head!=null){
//先将head的下一个节点存起来,避免之后造成链表中断
next=head.next;
//将前一个节点赋给此时节点的下一个节点,达到变向的效果
head.next=pre;
//将pre和head都往后移一位
pre=head;
head=next;
}
return pre;
}
}
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}