707 Design Linked List

Python

class MyNode(object):
    def __init__(self, val):
        self.val = val
        self.next = None

class MyLinkedList(object):

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

    def get(self, index):
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        :type index: int
        :rtype: int
        """
        if index < 0 or index >= self.length:
            return -1
        else:
            node = self.head
            for i in range(index):
                node = node.next
            return node.val

    def addAtHead(self, val):
        """
        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.
        :type val: int
        :rtype: None
        """
        node = MyNode(val)
        if self.head is None:
            self.head = node
        else:
            t = self.head
            self.head = node
            self.head.next = t
        self.length += 1

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: None
        """
        node = MyNode(val)
        if self.head is None:
            self.head = node
        else:
            head = self.head
            while head.next is not None:
                head = head.next
            head.next = node
        self.length += 1

    def addAtIndex(self, index, val):
        """
        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.
        :type index: int
        :type val: int
        :rtype: None
        """
        if index > self.length or (index < 0 and self.head is not None):
            return
        if index < 0 and self.head is None:
            self.addAtHead(val)
        elif index == self.length:
            self.addAtTail(val)
        elif index == 0:
            self.addAtHead(val)
        else:
            pre, node = None, self.head
            for i in range(index):
                pre = node
                node = node.next 
            newNode = MyNode(val)
            pre.next = newNode
            newNode.next = node
            self.length += 1

    def deleteAtIndex(self, index):
        """
        Delete the index-th node in the linked list, if the index is valid.
        :type index: int
        :rtype: None
        """
        if index >= self.length or index < 0:
            return
        if index == 0:
            self.head = self.head.next
        else:
            pre, node = None, self.head
            for i in range(index):
                pre = node
                node = node.next
            pre.next = node.next
        self.length -= 1

# 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)

Java

class MyNode{
    int val;
    MyNode next;

    public MyNode(int _val){
        val = _val;
        next = null;
    }
}
class MyLinkedList {
    MyNode head;
    int length;
    /** Initialize your data structure here. */
    public MyLinkedList() {
        length = 0;
        head = null;
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        if(index < 0 || index >= length){
            return -1;
        }
        MyNode node = head;
        for(int i = 0; i < index; i++){
            node = node.next;
        }
        return node.val;
    }

    /** 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. */
    public void addAtHead(int val) {
        MyNode node = new MyNode(val);
        node.next = head;
        head = node;
        length += 1;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        MyNode node = new MyNode(val);
        if(head == null){
            head = node;
        }
        else{
            MyNode p = head;
            while(p.next != null)
                p = p.next;
            p.next = node;
        }
        length += 1;
    }

    /** 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. */
    public void addAtIndex(int index, int val) {
       if ((index > length) || (index < 0 && head != null))
       {
           return;
       }
        if ((index < 0) && (head == null))
            addAtHead(val);
        else if (index == length)
            addAtTail(val);
        else if(index == 0)
            addAtHead(val);
        else{
            MyNode pre = null;
            MyNode node = head;
            for (int i = 0; i < index; i++){
                pre = node;
                node = node.next; 
            }
            MyNode newNode = new MyNode(val);
            pre.next = newNode;
            newNode.next = node;
            length += 1;
        }

    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if((index >= length) ||index < 0)
            return;
        if (index == 0)
            head = head.next;
        else
        {
            MyNode pre = null;
            MyNode node = head;
            for (int i = 0; i < index; i++){
                pre = node;
                node = node.next;
            }
            pre.next = node.next;
        }    
        length -= 1;
    }
}

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

results matching ""

    No results matching ""