题目
题解
题解思路:官方题解
双指针
public ListNode reverseList02(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null){
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
递归
// 递归写法
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null){
return head;
}
ListNode node = reverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}