DEV Community

Cover image for Leetcode Solution: #206: Reverse Linked List ๐Ÿฌ
Roshan Sharma
Roshan Sharma

Posted on

Leetcode Solution: #206: Reverse Linked List ๐Ÿฌ

Question Type: Medium ๐ŸŽš๏ธ
Complexities: Time: O(n), Space: O(n) ๐Ÿšฉ

Code: ๐Ÿ‘‡

class Solution {
 public:
  ListNode* reverseList(ListNode* head) {
    if (!head || !head->next)
      return head;

    ListNode* newHead = reverseList(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
  }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)