码迷,mamicode.com
首页 > 其他好文 > 详细

707. Design Linked List

时间:2018-11-20 16:29:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:get   根据   att   sign   UNC   private   http   esc   链表   

题目链接:https://leetcode.com/problems/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.

 

思路:

  • 根据题目要求完成相应函数接口的编写即可。
  • 注意考虑插入、删除操作的一些边界条件;也要注意链表的状态是否为空。考虑要全面,严谨!!!

 

编码如下

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

707. Design Linked List

标签:get   根据   att   sign   UNC   private   http   esc   链表   

原文地址:https://www.cnblogs.com/ming-1012/p/9989179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!