1. 程式人生 > 其它 >力扣 -設計連結串列 --單鏈表的實現

力扣 -設計連結串列 --單鏈表的實現

技術標籤:leetcode連結串列leetcode

題目描述

設計連結串列的實現。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具有兩個屬性:val 和 next。val 是當前節點的值,next 是指向下一個節點的指標/引用。如果要使用雙向連結串列,則還需要一個屬性 prev 以指示連結串列中的上一個節點。假設連結串列中的所有節點都是 0-index 的。

在連結串列類中實現這些功能:

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

提示:

所有val值都在 [1, 1000] 之內。
操作次數將在 [1, 1000] 之內。
請不要使用內建的 LinkedList 庫。

作者:力扣 (LeetCode)
連結:https://leetcode-cn.com/leetbook/read/linked-list/jy291/

來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

程式碼:

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x){val = x; }
}
class MyLinkedList {
    int size;
    ListNode head;
        

    /** Initialize your data structure here. */
    public MyLinkedList() {
        size = 0;
        //虛擬化一個頭節點
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        head = newHead;
    }
    

    
    /** 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 >= size){
            return -1;
        }
       ListNode curr = head;
       for(int i=0;i <= index ; i++){
           curr = curr.next;
       }
       return curr.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) {
        // addAtIndex(0,val);
        //直接寫
        ListNode newNode = new ListNode(val);
        newNode.next = head.next;

        head.next = newNode;

        size++;

    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        // addAtIndex(size,val);
        ListNode curr = head;
        while (curr.next != null){
            curr = curr.next;
        }
        ListNode newNode = new ListNode(val);
        newNode.next = null;
        curr.next = newNode;

        size++;
    }
    
    /** 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 >size){return; }
        if (index < 0){ index = 0;}

       
        ListNode curr = head;
        for(int i=0;i < index ; i++){
            curr = curr.next;
        }

        ListNode newNode =new ListNode(val);
        newNode.next = curr.next;
        curr.next = newNode;
        
         size++;
        

    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(index < 0 || index >=size) {return;}

        ListNode curr = head;
        for(int i=0;i < index; i++){
            curr = curr.next;
        }

        curr.next = curr.next.next;
        size--;

    }
}

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

注意:

  • 在刪除指定下標的節點。index = size 無效;
  • 增加/刪除節點時,修改size;
  • 虛擬化頭節點的使用;