题目描述
输入一个链表,反转链表后,输出新链表的表头。
/*
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 Inext = null;
ListNode Ipre = null;
while(head!=null){
if(head.next==null){
head.next=Ipre;
break;
}
Inext=head.next;
head.next=Ipre;
Ipre = head;
head=Inext;
}
return head;
}
}
尾节点直接链接上一节点
其他节点逆转方向
记录一下!