You are viewing a single comment's thread. Return to all comments →
From my HackerRank solutions.
Iterative Solution Runtime: O(n) Space Complexity: O(1)
Recursion is cool but using it on this problem will require O(n) space. Do it iteratively instead.
Node Delete(Node head, int position) { if (head == null) { return null; } else if (position == 0) { return head.next; } else { Node n = head; for (int i = 0; i < position - 1; i++) { n = n.next; } n.next = n.next.next; return head; } }
Let me know if you have any questions.
Seems like cookies are disabled on this browser, please enable them to open this website
Delete a Node
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Iterative Solution
Runtime: O(n)
Space Complexity: O(1)
Recursion is cool but using it on this problem will require O(n) space. Do it iteratively instead.
Let me know if you have any questions.