题目描述
输入一个链表,反转链表后,输出新链表的表头。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.util.ArrayList;
//import java.util.Stack;
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null){
return null;
}
ListNode pre=null;
ListNode after=null;
while(head!=null){
after=head.next;
head.next=pre;
pre=head;
head=after;
}
return pre;
}
}