反转链表

题目

牛客网

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

解题思路

  1. 三个指针
  1. public ListNode ReverseList(ListNode head) {
  2. if (head == null || head.next == null) {
  3. return head;
  4. }
  5. ListNode pre = head, cur = head.next, next;
  6. pre.next = null;
  7. while (cur != null) {
  8. next = cur.next;
  9. cur.next = pre;
  10. pre = cur;
  11. cur = next;
  12. }
  13. return pre;
  14. }