diff --git a/OJ notes/pages/Leetcode Reverse-Linked-List.md b/OJ notes/pages/Leetcode Reverse-Linked-List.md index 2a62888..c30f5a1 100644 --- a/OJ notes/pages/Leetcode Reverse-Linked-List.md +++ b/OJ notes/pages/Leetcode Reverse-Linked-List.md @@ -80,6 +80,8 @@ I thought a slow O(n ^ 2) hybrid solution, while there are better algorithms, us #### Fast way: +#### Recursion + The in place insert is easier to understand, and simple to implement, using a very clever trick. > [!summary] My thoughts on the recursion algorithm @@ -93,6 +95,15 @@ The in place insert is easier to understand, and simple to implement, using a ve > It returns the last element in the original list to make sure you are always returning the head of the reversed array, since for any reversed list, the last one comes first. > **The only recursive part is returning the _tail node_.** +##### Iteration + +The iteration method use two pointers: + +- one that points to the new head, as an anchor +- The other one points to nodes we need to iterate over: + + + ### Solution I've referred to this guy: https://leetcode.com/problems/reverse-linked-list/discuss/58130/C%2B%2B-Iterative-and-Recursive