mengyushi / LeetCode

4 stars 0 forks source link

707. Design Linked List #267

Open mengyushi opened 4 years ago

mengyushi commented 4 years ago
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class MyLinkedList:
    '''
    head->1->3->...6->None
                  tail

    '''

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = Node(0)
        self.tail = None
        self.size = 0

    def get(self, index: int) -> int:
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        """

        if index < 0 or index >= self.size:
            return -1

        pointer = self.head.next

        if not index:
            return self.head.next.val

        while index-1:
            pointer = pointer.next
            index-=1

        return pointer.next.val

    def addAtHead(self, val: int) -> None:
        """
        Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
        """
        n = Node(val)
        pre_head = self.head.next
        self.head.next = n
        n.next = pre_head
        if not self.size:
            self.tail = n
        self.size+=1

    def addAtTail(self, val: int) -> None:
        """
        Append a node of value val to the last element of the linked list.
        """
        if self.size == 0:
            return self.addAtHead(val)

        self.tail.next = Node(val)
        self.tail = self.tail.next
        self.size+=1

    def addAtIndex(self, index: int, val: int) -> None:
        """
        Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
        """
        if index < 0 or index > self.size:
            return

        elif not index:
            return self.addAtHead(val)

        elif index == self.size:
            return self.addAtTail(val)

        pointer = self.head.next
        while index-1:
            pointer = pointer.next
            index-=1

        pre_next = pointer.next
        pointer.next = Node(val)
        pointer.next.next = pre_next
        self.size+=1

    def deleteAtIndex(self, index: int) -> None:
        """
        Delete the index-th node in the linked list, if the index is valid.
        """

        if index < 0 or index > self.size:
            return

        elif not index:
            self.head.next = self.head.next.next
            if self.size == 1:
                self.tail = None
            self.size -=1
            return

        pointer = self.head.next
        while (index-1):
            pointer = pointer.next

        pointer.next = pointer.next.next
        self.size-=1
        return

# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)