Design list

Implement the design list. You can choose to use single or double-linked list. Single node in the list should have two attributes: val and next. and val is the value of the current node, next pointing to the next node pointer / reference. To use a doubly linked list, an attribute is also required to prev one node indicating the list. Suppose all the nodes in the list are the 0-index.

Implementing these features in the list class:

get (index): Gets the value of the index in the list of nodes. If the index is invalid, -1 is returned.
addAtHead (val): adding a node to val before the first element in the list. After insertion, the new node will be the first node of the list.
addAtTail (val): the node is added to the value val is the last element of the list.
addAtIndex (index, val): adding a node to val before the list of index nodes. If the index is equal to the length of the list, then the node will be appended to the end of the list. If the index is greater than the length of the list, the node will not be inserted. If the index is less than 0, then the node is inserted in the head.
deleteAtIndex (index): If the index index is valid, delete the list of index nodes.
 

Example:

= New new MyLinkedList linkedList MyLinkedList ();
linkedList.addAtHead (. 1);
linkedList.addAtTail (. 3);
linkedList.addAtIndex (1,2); // list becomes l-> 2->. 3
linkedList.get (. 1); // returns 2
linkedList.deleteAtIndex (. 1); // list is now l-> 3
linkedList.get (. 1); // returns 3
 

prompt:

All values are within the val [1, 1000] it.
The number of operations in [1, 1000] within.
Do not use the built-in LinkedList library.

Solution 1:

  

class MyLinkedList {
    
public static class ListNode {
    private int val;
    private ListNode next;

    public ListNode(int val) {
      this.val = val;
    }
  }
    /** Initialize your data structure here. */
    public MyLinkedList() {
        
    }
    
    private ListNode listNode=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(listNode==null){
            return -1;
        }
        ListNode itr=this.listNode;
        int count=0;
        while(itr!=null){
            if(count==index){
                return itr.val;
            }
            count++;
            itr=itr.next;
        }
        return -1;
    }
    
    /** 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) {
        ListNode newNode=new ListNode(val);
        newNode.next=this.listNode;
        this.listNode=newNode;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        ListNode itr=this.listNode;
        ListNode last=null;
        ListNode newNode=new ListNode(val);
        while(itr!=null){
            if(itr.next==null){
                last=itr;
            }
            itr=itr.next;
        }
        if(last!=null){
            last.next=newNode;
        }else{
            this.listNode=newNode;
        }
    }
    
    /** 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<=0){
            addAtHead(val);
        }else{
         ListNode itr=this.listNode;
        int count=0;
        ListNode newNode=new ListNode(val);
        while(itr!=null){
            if(count+1==index){
                ListNode tmp=itr.next;
                itr.next=newNode;
                if(tmp!=null){
                    newNode.next=tmp;
                }
                break;
            }
            count++;
            itr=itr.next;
        }   
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        ListNode itr=this.listNode;
        int count=0;
        ListNode prev=null;
        while(itr!=null){
            if(count==index){
                ListNode tmp=itr.next;
                if(prev!=null){
                 prev.next=tmp;   
                }else{
                 this.listNode=tmp;
                }
                break;
            }
            count++;
            prev=itr;
            itr=itr.next;
        }
    }
}

/**
 * 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);
 */
View Code

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/design-linked-list

Guess you like

Origin www.cnblogs.com/wuyouwei/p/11826761.html