A straightforward 14-line C++ solution in 12 ms


  • 0
       ListNode* reverseList(ListNode* head) {
                if (head == NULL || head -> next == NULL) return head;
                ListNode* new_head = new ListNode(0);
                ListNode* pre = new_head;
                new_head -> next = head;
                ListNode* cur = head;
                while (cur -> next) {
                    ListNode* move = cur -> next;
                    cur -> next = move -> next;
                    move -> next = pre -> next;
                    pre -> next = move;
                }
                return new_head -> next;
            }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.