leetcode/src/main/java/com/my/MyLinkedList.java
/** 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. */
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.
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 > listIndex){
        }else if(index == listIndex){
            addAtTail(val);
        }else if(index<=0){
            addAtHead(val);
            listIndex ++;
        }else {
            SingleNode temp = head;
            for (int i =1 ;i<index;i++){
                if(temp.next != null){
                    temp = temp.next;
                }
            }
            SingleNode nextTemp = temp.next;
            SingleNode node = new SingleNode(val);
            temp.next = node;
            node.next = nextTemp;
            listIndex ++;
        }
    }
public void addAtIndex ( int index , int val ) { if ( index > listIndex ) { } else if ( index == listIndex ) { addAtTail ( val ) ; } else if ( index <= 0 ) { addAtHead ( val ) ; listIndex ++ ; } else { SingleNode temp = head ; for ( int i = 1 ; i < index ; i ++ ) { if ( temp . next != null ) { temp = temp . next ; } } SingleNode nextTemp = temp . next ; SingleNode node = new SingleNode ( val ) ; temp . next = node ; node . next = nextTemp ; listIndex ++ ; } }
Add a node of value val before the index-th node in the linked list.
Add a node of value val before the index - th node in the linked list .