JAVA OJ練習第12題——設計鏈表

力扣鏈接:707. 設計鏈表

題目要求:在鏈表類中實現這些功能:

get(index):獲取鏈表中第 index 個節點的值。如果索引無效,則返回-1。
addAtHead(val):在鏈表的第一個元素之前添加一個值爲 val 的節點。插入後,新節點將成爲鏈表的第一個節點。
addAtTail(val):將值爲 val 的節點追加到鏈表的最後一個元素。
addAtIndex(index,val):在鏈表中的第 index 個節點之前添加值爲 val 的節點。如果 index 等於鏈表的長度,則該節點將附加到鏈表的末尾。如果 index 大於鏈表長度,則不會插入節點。如果index小於0,則在頭部插入節點。
deleteAtIndex(index):如果索引 index 有效,則刪除鏈表中的第 index 個節點。

示例:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //鏈表變爲1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //現在鏈表是1-> 3
linkedList.get(1);            //返回3

代碼如下:

class MyLinkedList {
    
    Node head;
    int length;

    /** Initialize your data structure here. */
    public MyLinkedList() {
        length = 0;
        head = new Node(-1);
    }
    
    /** 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 >= length || index < 0)
            return -1;
        Node current = head;
        for(int i = 0; i <= index; i++)
            current = current.next;
        return current.value;
    }
    
    /** 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) {
        Node temp = new Node(val);
        temp.next = head.next;
        head.next = temp;
        length ++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node current = head;
        while(current.next != null)
            current = current.next;
        Node temp = new Node(val);
        current.next = temp;
        length ++;
    }
    
    /** 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)
            return;
        if(index == length){
            addAtTail(val);
            return;
        }
        if(index < 0)
            index = index+length+1;
        Node current = head;
        for(int i = 0; i < index; i++)
            current = current.next;
        Node ptr = current.next;
        Node temp = new Node(val);
        current.next = temp;
        temp.next = ptr;
        length ++;
    }
    
    /** 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;
        Node current = head;
        for(int i = 0; i < index; i++)
            current = current.next;
        current.next = current.next.next;
        length --;
    }
    
    public void print(){
        Node current = head;
        StringBuilder builder = new StringBuilder();
        while(current != null){
            builder.append(String.valueOf(current.value) + " ");
            current = current.next;
        }
        System.out.println(builder);
    }
}

class Node{
    int value;
    Node next;
    
    public Node(int value){
        this.value = value;
    }
}

下一題:二維網格的遷移
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章