O(n) time O(1) space Ruby Solution, single pass


  • 0
    S
    def remove_nth_from_end(head, n)
        return nil unless head.next
        count = 0
        h, p = head, head
        while h
            p = p.next if count > n
            h = h.next
            count += 1
        end
        return head.next if count == n
        p.next = p.next.next
        head
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.