Skip to content

Latest commit

 

History

History
30 lines (21 loc) · 634 Bytes

reverse_ll.md

File metadata and controls

30 lines (21 loc) · 634 Bytes

206. Reverse Linked List, Easy

  • Classic type of question, and a must know problem.
  • Elegant use of 3 pointers.
  • You should first dry run on notebook.
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr or head -> next == nullptr) 
            return head;

        ListNode* a = nullptr;
        ListNode* b = head;
        ListNode* c = b -> next;

        while (c != nullptr) {
            b -> next = a;

            a = b; 
            b = c;

            c = c -> next;
        }
        
        b -> next = a;
        return b;
    }