使用递归
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
现转
ListNode curr = head;
ListNode pre = head, prepre = null;
while(curr != null) {
pre = curr;
curr = curr.next;
pre.next = prepre;
prepre = pre;
}
return pre;
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
ListNode next1 = null;
while(head!=null){
next1 = head.next;
head.next = newHead;
newHead = head;
head = next1;
}
return newHead;
}