反转链表

题目

牛客网

输入一个链表,反转链表后,输出新链表的表头。

解题思路

  1. 三个指针
public ListNode ReverseList(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }

    ListNode pre = head, cur = head.next, next;
    pre.next = null;

    while (cur != null) {
        next = cur.next;
        cur.next = pre;

        pre = cur;
        cur = next;
    }

    return pre;
}