递归
public static ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newNode = reverseList(head.next);
head.next.next = head;
head.next = null;
return newNode;
}
迭代
头插法
public static ListNode reverseList(ListNode head) {
ListNode newHead = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next;
current.next = newHead;
newHead = current;
current = nextNode;
}
return newHead;
}