链表

简介:


 

单链表中的每一个结点不只包含值,还包含连接到下一个结点的引用字段。经过这种方式,单链表将全部结点按顺序组织起来。下面是一个单链表的例子:node

蓝色箭头显示单个连接列表中的结点是如何组合在一块儿的。app


节点结构:


 

1 // Definition for singly-linked list.
2 struct SinglyListNode {
3     int val;
4     SinglyListNode *next;
5     SinglyListNode(int x) : val(x), next(NULL) {}
6 };

设计链表:


 

设计链表的实现。您能够选择使用单链表或双链表。单链表中的节点应该具备两个属性:val 和 nextval 是当前节点的值,next 是指向下一个节点的指针/引用。若是要使用双向链表,则还须要一个属性 prev 以指示链表中的上一个节点。假设链表中的全部节点都是 0-index 的。spa

在链表类中实现这些功能:设计

  • 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 库。

代码:

  1 class MyLinkedList 
  2 {
  3 public:
  4     /** Initialize your data structure here. */
  5     MyLinkedList() : head(nullptr) {} 
  6     
  7     /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  8     int get(int index) 
  9     {
 10         if (head != nullptr)
 11         {
 12             Node *now = head;
 13             int nownum = 0;
 14             while (now->next != nullptr && nownum < index)
 15             {
 16                 now = now->next;
 17                 nownum++;
 18             }
 19             if (nownum == index)
 20                 return now->val;
 21         }
 22         return -1;
 23     }
 24     
 25     /** 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. */
 26     void addAtHead(int val) 
 27     {
 28         Node *temp = new Node(val);
 29         temp->next = head;
 30         head = temp;
 31     }
 32     
 33     /** Append a node of value val to the last element of the linked list. */
 34     void addAtTail(int val) 
 35     {
 36         Node *temp = new Node(val);
 37         if (head == nullptr)
 38             head = temp;
 39         else
 40         {
 41             Node *now = head;
 42             while (now->next != nullptr)
 43                 now = now->next;
 44             now->next = temp;
 45         }
 46     }
 47     
 48     /** 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. */
 49     void addAtIndex(int index, int val) 
 50     {
 51         Node *temp = new Node(val);
 52         if (index <= 0)
 53         {
 54             temp->next = head;
 55             head = temp;
 56         }
 57         else if (head != nullptr)
 58         {
 59             Node *pre = head;
 60             int prenum = 0;
 61             while (pre->next != nullptr && prenum < index - 1)
 62             {
 63                 pre = pre->next;
 64                 prenum++;
 65             }
 66             if (prenum == index - 1)
 67             {
 68                 temp->next = pre->next;
 69                 pre->next = temp;
 70             }
 71         }
 72     }
 73     
 74     /** Delete the index-th node in the linked list, if the index is valid. */
 75     void deleteAtIndex(int index) 
 76     {
 77         if (head != nullptr)
 78         {
 79             if (index == 0)
 80             {
 81                 Node *temp = head;
 82                 head = head->next;
 83                 delete temp;
 84             }
 85             else
 86             {
 87                 Node *pre = head;
 88                 int prenum = 0;
 89                 while (pre->next != nullptr && prenum < index - 1)
 90                 {
 91                     pre = pre->next;
 92                     prenum++;
 93                 }
 94                 if (pre->next != nullptr && prenum == index - 1)
 95                 {
 96                     Node *temp = pre->next;
 97                     pre->next = temp->next;
 98                     delete temp;
 99                 }
100             }
101         }
102     }
103     
104 private:
105     struct Node
106     {
107         int val;
108         struct Node *next;
109         Node(int x) : val(x), next(nullptr) {}
110     };
111     struct Node *head;
112 };
113 
114 /**
115  * Your MyLinkedList object will be instantiated and called as such:
116  * MyLinkedList* obj = new MyLinkedList();
117  * int param_1 = obj->get(index);
118  * obj->addAtHead(val);
119  * obj->addAtTail(val);
120  * obj->addAtIndex(index,val);
121  * obj->deleteAtIndex(index);
122  */
相关文章
相关标签/搜索