🤖
Data Structures and Algorithms
  • CS Theory And Problems
  • Big O
    • Big O Notation
  • Algorithms
    • Binary Search
    • Breadth First Search (BFS)
    • Depth First Search (DFS)
    • Dynamic Programming
    • Kadane's Algorithm
    • Kahn's Algorithm
    • Quickselect
    • Recursion
    • Sorting Algorithms
    • Sliding Window
  • Data Structures
    • Binary Heap
    • Graph
    • Linked List
    • Trees
  • Problems
    • LeetCode
      • 1. Two Sum
      • 3. Longest Substring Without Repeating Characters
      • 5. Longest Palindromic Substring
      • 11. Container With Most Water
      • 15. 3 Sum
      • 19. Remove Nth Node From End of List
      • 20. Valid Parentheses
      • 33. Search in Rotated Sorted Array
      • 49. Group Anagrams
      • 53. Maximum Subarray
      • 55. Jump Game
      • 56. Merge Intervals
      • 76. Minimum Window Substring
      • 98. Validate Binary Search Tree
      • 105. Construct Binary Tree from Preorder and Inorder Traversal
      • 121. Best Time to Buy and Sell Stock
      • 133. Clone Graph
      • 141. Linked List Cycle
      • 152. Maximum Product Subarray
      • 153. Find Minimum in Rotated Sorted Array
      • 200. Number of Islands
      • 206. Reverse Linked List
      • 207. Course Schedule
      • 217. Contains Duplicate
      • 226. Invert Binary Tree
      • 238. Product of Array Except Self
      • 242. Valid Anagram
      • 297. Serialize and Deserialize Binary Tree
      • 347. Top K Frequent Elements
      • 417. Pacific Atlantic Water Flow
      • 424. Longest Repeating Character Replacement
      • 435. Non-overlapping Intervals
      • 647. Palindromic Substrings
Powered by GitBook
On this page
  • Description
  • Solution
  1. Problems
  2. LeetCode

19. Remove Nth Node From End of List

Description

See: https://leetcode.com/problems/remove-nth-node-from-end-of-list/

Solution

I used the runner technique to solve this problem. I move the runner forward n times. Then I move both the walker and runner together at the same time. When the runner gets to the end of the linked list, the walker will be at the node prior to the node to be removed. Then it is a matter of updating the walker's next pointer.

My first version was messier than this. I had one while loop and a counter I incremented in it. If the counter was greater than n then I would move the walker. It worked, but this is tidier.

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        walker = runner = head
        
        # move the runner forward the n spots. This will leave a gap between the walker
        # and runner of the desired "back n elements"
        for i in range(n):
            runner = runner.next
        
        # Handles the case where n is the same size as the linked list. The runner reaches 
        # the end of the linked list, so just return the head's next item.
        if not runner:
            return head.next
        
        # Now move runner through the rest of the linked list, walker moves along too
        while runner.next:
            runner = runner.next
            walker = walker.next
        
        # Remove the "nth node from the end of the list" by updating the prior node's
        # next pointer                
        walker.next = walker.next.next
        return head
        
Previous15. 3 SumNext20. Valid Parentheses

Last updated 3 years ago