描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入:
{1,2,3}
复制返回值:
{3,2,1}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode beg=null;
ListNode end=null;
//beg作为新的表头,head保存原表头
beg=head;
if(head==null)
return null;
while(head.next!= null){
end=head.next;
head.next=end.next;
end.next=beg;
beg=end;
}
return beg;
}
}
//1-2-3-4 直接断链翻转