Leetcode 206 Solution

This article provides solution to leetcode question 206 (reverse-linked-list)

https://leetcode.com/problems/reverse-linked-list

Solution

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = NULL;
        ListNode* p = head;

        while (p)
        {
            auto next = p->next;
            p->next = prev;
            prev = p;
            p = next;
        }

        return prev;
    }
};