【LeetCode】22.Linked List — Design Linked List 设计链表(链表的定义、初始化及插入删除操作)

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and nextval is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(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.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(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.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

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

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in LinkedList library.

这里给出链表的类定义,包括结点的定义,插入删除操作。

// Definition for singly-linked list.
struct Node {
    int val;
    Node *next;
//    Node(int x) :val(x), next(NULL) {} //结构体构造函数,仅限C++内使用
};

class MyLinkedList {
    Node *head;    //链表头
    Node *tail;    //链表尾 
    int length;    //链表长度
public:
    /** Initialize your data structure here. */
    MyLinkedList() {  //类默认构造函数,不显示声明时提供初始化操作
        head = NULL;
        tail = NULL;
        length = 0; 
    }

    /** Get the value of the index-th Node in the linked list. If the index is invalid, return -1. */
    int get(int index) {  //获取指定下标的值
        Node*p = head;
        if (index < 0 || index >= length) return -1;
        for (int i = 0; i<index; i++) //从头结点开始遍历,找到指定的值
        {
            p = p->next;
        }
        return p->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. */
    void addAtHead(int val) {  //头部插入,需要定义新的结点,更新length值
        Node*Add1 = new Node;  //定义构造函数后,则初始化方式变为:Node*Add1= new Node(val);
        Add1->val = val;
        Add1->next = head;
        head = Add1;
        length++;
        if (length == 1) tail = head;

    }

    /** Append a Node of value val to the last element of the linked list. */
    void addAtTail(int val) { //尾部插入,定义新结点,判断特殊情况,更新length值
        Node*Add2 = new Node;
        Add2->val = val;
        if (tail != NULL)     tail->next = Add2;
        tail = Add2;
        length++;
        if (length == 1) head = tail;
    }

    /** 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. */
    void addAtIndex(int index, int val) { //在特定下标前插入结点,分情况讨论,更新length的值
        if (index > length) return;
        if (index == length) {
            addAtTail(val);
            return;
        }
        if (index <= 0) {
            addAtHead(val);
            return;
        }
        Node*p = head;
        for (int i = 0; i < index - 1; i++)
        {
            p = p->next;
        }
        Node*Add3 = new Node;
        Add3->val = val;
        Add3->next = p->next;
        p->next = Add3;
        length++;
    }

    /** Delete the index-th Node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) { //删除指定下标结点
        if (index >= length || index<0) return;
        Node*p = head;
        if (index == 0) {
            head = head->next;
            delete p;
            length--;
            return;
        }
        for (int i = 0; i < index - 1; i++)
        {
            p = p->next;
        }
        Node*Delete = p->next; //要删除的节点
        p->next = Delete->next;
        if (tail == Delete)tail = p;
        delete Delete;
        length--;
    }
};

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

 

posted @ 2019-06-21 09:24  蓝天下的一棵草  阅读(353)  评论(0编辑  收藏  举报