<reponame>dhivadeekshi/Algorithms-Samples
"""The LinkedList code from before is provided below.
Add three functions to the LinkedList.
"get_position" returns the element at a certain position.
The "insert" function will add an element to a particular
spot in the list.
"delete" will delete the first element with that
particular value.
Then, use "Test Run" and "Submit" to run the test cases
at the bottom."""


class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None


class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    def append(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:
            self.head = new_element

    def __get__(self, node, counter):
        if node is None:
            return None

        if counter == 0:
            return node

        return self.__get__(node.next, counter - 1)

    def get_position(self, position):
        """Get an element from a particular position.
        Assume the first position is "1".
        Return "None" if position is not in the list."""
        return self.__get__(self.head, position - 1)

    def __insert__(self, node, new_element, counter):
        if node is None:
            return new_element

        if counter == 0:
            new_element.next = node
            return new_element

        node.next = self.__insert__(node.next, new_element, counter - 1)
        return node

    def insert(self, new_element, position):
        """Insert a new node at the given position.
        Assume the first position is "1".
        Inserting at position 3 means between
        the 2nd and 3rd elements."""
        self.head = self.__insert__(self.head, new_element, position - 1)

    def __delete_position__(self, node, value):
        if node is None:
            return None

        if node.value == value:
            return node.next

        node.next = self.__delete_position__(node.next, value)
        return node

    def delete(self, value):
        self.head = self.__delete_position__(self.head, value)


if __name__ == '__main__':

    # Test cases
    # Set up some Elements
    e1 = Element(1)
    e2 = Element(2)
    e3 = Element(3)
    e4 = Element(4)

    # Start setting up a LinkedList
    ll = LinkedList(e1)
    ll.append(e2)
    ll.append(e3)

    # Test get_position
    # Should print 3
    print(ll.head.next.next.value)
    # Should also print 3
    print(ll.get_position(3).value)

    # Test insert
    ll.insert(e4, 3)
    # Should print 4 now
    print(ll.get_position(3).value)

    # Test delete
    ll.delete(1)
    # Should print 2 now
    print(ll.get_position(1).value)
    # Should print 4 now
    print(ll.get_position(2).value)
    # Should print 3 now
    print(ll.get_position(3).value)

"""The LinkedList code from before is provided below.
Add three functions to the LinkedList.
"get_position" returns the element at a certain position.
The "insert" function will add an element to a particular
spot in the list.
"delete" will delete the first element with that
particular value.
Then, use "Test Run" and "Submit" to run the test cases
at the bottom."""


class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None


class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    def append(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:
            self.head = new_element

    def __get__(self, node, counter):
        if node is None:
            return None

        if counter == 0:
            return node

        return self.__get__(node.next, counter - 1)

    def get_position(self, position):
        """Get an element from a particular position.
        Assume the first position is "1".
        Return "None" if position is not in the list."""
        return self.__get__(self.head, position - 1)

    def __insert__(self, node, new_element, counter):
        if node is None:
            return new_element

        if counter == 0:
            new_element.next = node
            return new_element

        node.next = self.__insert__(node.next, new_element, counter - 1)
        return node

    def insert(self, new_element, position):
        """Insert a new node at the given position.
        Assume the first position is "1".
        Inserting at position 3 means between
        the 2nd and 3rd elements."""
        self.head = self.__insert__(self.head, new_element, position - 1)

    def __delete_position__(self, node, value):
        if node is None:
            return None

        if node.value == value:
            return node.next

        node.next = self.__delete_position__(node.next, value)
        return node

    def delete(self, value):
        self.head = self.__delete_position__(self.head, value)


if __name__ == '__main__':

    # Test cases
    # Set up some Elements
    e1 = Element(1)
    e2 = Element(2)
    e3 = Element(3)
    e4 = Element(4)

    # Start setting up a LinkedList
    ll = LinkedList(e1)
    ll.append(e2)
    ll.append(e3)

    # Test get_position
    # Should print 3
    print(ll.head.next.next.value)
    # Should also print 3
    print(ll.get_position(3).value)

    # Test insert
    ll.insert(e4, 3)
    # Should print 4 now
    print(ll.get_position(3).value)

    # Test delete
    ll.delete(1)
    # Should print 2 now
    print(ll.get_position(1).value)
    # Should print 4 now
    print(ll.get_position(2).value)
    # Should print 3 now
    print(ll.get_position(3).value)
The LinkedList code from before is provided below. Add three functions to the LinkedList. "get_position" returns the element at a certain position. The "insert" function will add an element to a particular spot in the list. "delete" will delete the first element with that particular value. Then, use "Test Run" and "Submit" to run the test cases at the bottom. Get an element from a particular position.         Assume the first position is "1".         Return "None" if position is not in the list. Insert a new node at the given position.         Assume the first position is "1".         Inserting at position 3 means between         the 2nd and 3rd elements. # Test cases # Set up some Elements # Start setting up a LinkedList # Test get_position # Should print 3 # Should also print 3 # Test insert # Should print 4 now # Test delete # Should print 2 now # Should print 4 now # Should print 3 now