Leetcode 206 Solution
This article provides solution to leetcode question 206 (reverse-linked-list)
Access this page by simply typing in "lcs 206" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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;
}
};