输入一个链表,反转链表后,输出新链表的表头。
用的循环,但是实际上用栈和数组也是可以的
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public static ListNode ReverseList(ListNode head) {
ListNode cur=head;
ListNode pre=null;
ListNode temp=null;
while(cur!=null){
temp=cur;
cur=cur.next;
temp.next=pre;
pre=temp;
}
return pre;
}
}