minimanimoh / Udacity_DS-A

0 stars 0 forks source link

DS&A - 2. Data Structures - Lesson 1. Skip i, delete j #13

Open minimanimoh opened 3 years ago

minimanimoh commented 3 years ago

Problem Statement

You are given the head of a linked list and two integers, i and j. You have to retain the first i nodes and then delete the next j nodes. Continue doing so until the end of the linked list.

Example:

def skip_i_delete_j(head, i, j):
    """
    :param: head - head of linked list
    :param: i - first `i` nodes that are to be skipped
    :param: j - next `j` nodes that are to be deleted
    return - return the updated head of the linked list
    """
    if i == 0:
        return None

    if head is None or j < 0 or i < 0:
        return head

    current = head
    previous = None

    while current:
        for num in range(i - 1):
            if current is None:
                return head
            current = current.next
minimanimoh commented 3 years ago

A2E55DCC-22E7-4D22-991E-2006CE327043