|
| 1 | +/** |
| 2 | + * @see {@link https://leetcode.com/problems/reverse-linked-list/?envType=study-plan&id=level-1 206. Reverse Linked List} |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * EXPLAIN |
| 7 | + * |
| 8 | + * Start at head of an LL, SINGLY LINKED |
| 9 | + * Create a LL, fill it with the original LL vals, reversed |
| 10 | + * return that reversed LL |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * APPROACH |
| 15 | + * |
| 16 | + * RECURSION, create a stack to get to the end of the LL |
| 17 | + * Recurse backwards to get the vals in reverse order |
| 18 | + * |
| 19 | + * WHILE HAS NEXT |
| 20 | + * return func (curr.next) |
| 21 | + * |
| 22 | + * DOESN'T HAVE NEXT |
| 23 | + * new LL val is curr val |
| 24 | + * new LL next is new node |
| 25 | + * advance new LL ref to next |
| 26 | + * return original LL |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * CODE |
| 31 | + */ |
| 32 | +/** |
| 33 | + * Definition for singly-linked list. |
| 34 | + * function ListNode(val, next) { |
| 35 | + * this.val = (val===undefined ? 0 : val) |
| 36 | + * this.next = (next===undefined ? null : next) |
| 37 | + * } |
| 38 | + */ |
| 39 | +/** |
| 40 | + * @param {ListNode} head |
| 41 | + * @return {ListNode} |
| 42 | + */ |
| 43 | +var reverseList = function (currNode, prevNode) {}; |
| 44 | + |
| 45 | +/** |
| 46 | + * COMPLEXITY |
| 47 | + * TIME O() |
| 48 | + * SPACE O() |
| 49 | + */ |
| 50 | + |
| 51 | +/** |
| 52 | + * READ 0 |
| 53 | + * EXPLAIN 0 |
| 54 | + * APPROACH 0 |
| 55 | + * CODE [FAIl] 0 |
| 56 | + * TEST 0 |
| 57 | + * OPTIMIZE 0 |
| 58 | + */ |
| 59 | + |
| 60 | +/** |
| 61 | + * REVIEW |
| 62 | + * |
| 63 | + * ITERATIVE EXPLANATION |
| 64 | + * TIME O(n) depends of length of list |
| 65 | + * SPACE O(1) just pointers, overwriting existing list next ref |
| 66 | + * |
| 67 | + * use two pointer curr and prev |
| 68 | + * assign curr to head, prev to null |
| 69 | + * |
| 70 | + * WHILE current isnt null |
| 71 | + * |
| 72 | + * temp store next node as next |
| 73 | + * rewrite curr next to prev |
| 74 | + * |
| 75 | + * update prev to curr node |
| 76 | + * update curr to next node |
| 77 | + * END WHILE |
| 78 | + * |
| 79 | + * return prev, which is now the head |
| 80 | + */ |
| 81 | +var reverseList = function (head) { |
| 82 | + let curr = head; |
| 83 | + let prev = null; |
| 84 | + |
| 85 | + while (curr !== null) { |
| 86 | + // store next node |
| 87 | + let next = curr.next; |
| 88 | + |
| 89 | + // reverse link |
| 90 | + curr.next = prev; |
| 91 | + |
| 92 | + // update pointer |
| 93 | + prev = curr; |
| 94 | + curr = next; |
| 95 | + } |
| 96 | + return prev; |
| 97 | +}; |
| 98 | +/** |
| 99 | + * RECURSIVE EXPLANATION |
| 100 | + * TIME O(n) n is length of LL |
| 101 | + * SPACE O(n) is number of calls on the stack |
| 102 | + * |
| 103 | + * Adding another variable to the problem was the right intuition. It was the PREV ref, not the whole list that needed to be passed. |
| 104 | + * |
| 105 | + * Steps go like this: |
| 106 | + * |
| 107 | + * reverseList accepts the current node, and the ref to the previous node |
| 108 | + * IF there's no node, return prev, starting the recursive chain |
| 109 | + * |
| 110 | + * define the next recursion's next as the next node |
| 111 | + * assign the value of curr node next to prev node ref |
| 112 | + * return recursive call with next as curr node, curr node as prev |
| 113 | + */ |
| 114 | +var reverseList = function (head, prev = null) { |
| 115 | + if (!head) return prev; |
| 116 | + let next = head.next; |
| 117 | + head.next = prev; |
| 118 | + return reverseList(next, head); |
| 119 | +}; |
0 commit comments