A recursive solution in C++ (27ms)


  • 0
    F
    class Solution {
    public:
        ListNode *reverseList(ListNode *head) {
            return f(head, NULL);
        }
    
        ListNode *f(ListNode *cur, ListNode *pre){
            if(not cur)
                return pre;
            ListNode *post = cur->next;
            cur->next = pre;
            return f(post, cur);
        }
    };

Log in to reply
 

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