|
| 1 | +# 19 - Remove Nth Node From List |
| 2 | + |
| 3 | +Difficulty: medium |
| 4 | +Done: Yes |
| 5 | +Last edited: March 3, 2022 4:53 PM |
| 6 | +Link: https://leetcode.com/problems/remove-nth-node-from-end-of-list/solution/ |
| 7 | +Topic: linked list, two pointers |
| 8 | + |
| 9 | +## Problem |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head. |
| 14 | + |
| 15 | +``` |
| 16 | +Input: head = [1,2,3,4,5], n = 2 |
| 17 | +Output: [1,2,3,5] |
| 18 | +``` |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +From intuition the first process that comes to mind is traversing the list to figure out the length `L`. Once we have length we can get the difference between `n` and length, which should signify the node position to remove. Then we can traverse the list a second time and remove the node after $`L-n`$ steps in the traversal. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Furthermore we will need define a counter after 1st traversal for finding length, and two pointers to keep track of *current* and *previous* once we perform 2nd traversal for removal of `L-n` node. Once we’re at `L-n` steps and remove the node, we can simply break and return head |
| 31 | + |
| 32 | +## Whiteboard |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +Find length and incrementing counter L. |
| 37 | + |
| 38 | +```python |
| 39 | +l = 0 |
| 40 | +while current is not none: |
| 41 | + current = current.next |
| 42 | + l += 1 |
| 43 | +``` |
| 44 | + |
| 45 | +once current is at L-n we move pointers, then we can break and return head |
| 46 | + |
| 47 | +```python |
| 48 | +while current is not None: |
| 49 | + if counter == L-n: |
| 50 | + previous.next = current.next |
| 51 | + return head |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +## Code |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +```python |
| 61 | +# Definition for singly-linked list. |
| 62 | +# class ListNode: |
| 63 | +# def __init__(self, val=0, next=None): |
| 64 | +# self.val = val |
| 65 | +# self.next = next |
| 66 | +class Solution: |
| 67 | + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: |
| 68 | + |
| 69 | + l = 0 |
| 70 | + current = head |
| 71 | + |
| 72 | + while current is not None: |
| 73 | + current = current.next |
| 74 | + l += 1 |
| 75 | + |
| 76 | + current = head |
| 77 | + previous = ListNode(-1) |
| 78 | + counter = 0 |
| 79 | + |
| 80 | + if l == n: |
| 81 | + # edge case where we only have 1 list node |
| 82 | + previous.next = current.next |
| 83 | + return previous.next |
| 84 | + |
| 85 | + while current is not None: |
| 86 | + if counter == l-n: |
| 87 | + # once we reach nth from end |
| 88 | + previous.next = current.next |
| 89 | + current = previous.next |
| 90 | + return head |
| 91 | + |
| 92 | + else: |
| 93 | + #keep traversing |
| 94 | + previous = current |
| 95 | + current = current.next |
| 96 | + |
| 97 | + counter += 1 |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + return previous.next |
| 102 | +``` |
| 103 | + |
| 104 | +## Time Complexity |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +Having traversed the list the first time to find `L` will take linear time O(n), and the second traversal to remove the `L-n` would also take linear time. Resulting in $O(n) + O(n) = O(2n)$, which can be asymptotically equivalent to $O(n)$ runtime. |
0 commit comments