Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Reverse linked list

  • 🧩 Problem link: Leetcode
  • 🚦 Difficulty: 🟢 Easy

💡 Approach

  • Set curr->next node to be the prev one, always updating in each loop prev with curr node
  • But using recursion

🕒 Time and Space Complexity

  • Time: O(n)
  • Space: O(n)

✅ Solution

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head) {
            return nullptr;
        }

        ListNode* newHead = head;
        if (head->next) {
            newHead = reverseList(head->next);
            head->next->next = head;
        }
        head->next = nullptr;

        return newHead;
    }
};