迭代
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head==null||head.next==null){
return head;
}
ListNode newHead = null;
while(head.next!=null){
ListNode tem = head;
head = head.next;
tem.next = newHead;
newHead = tem;
}
head.next = newHead;
return head;
}
}
递归
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head==null||head.next==null){
return head;
}
ListNode res = ReverseList(head.next);
head.next.next = head;
head.next = null;
return res;
}
}