707. Design Linked List
Posted ming-1012
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了707. Design Linked List相关的知识,希望对你有一定的参考价值。
题目链接: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 next
. val
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 theindex
-th node in the linked list. Ifindex
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的主要内容,如果未能解决你的问题,请参考以下文章
707. Design Linked List - Easy
LeetCode 0707. 设计链表:C++小详解,万字小长文:分别借助和不借助STL解决
算法分析如何理解快慢指针?判断linked list中是否有环找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycl(代码
LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II