赞
踩
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val
和 next
。val
是当前节点的值,next
是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev
以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
index
个节点的值。如果索引无效,则返回-1
。val
的节点。插入后,新节点将成为链表的第一个节点。val
的节点追加到链表的最后一个元素。index
个节点之前添加值为 val
的节点。如果 index
等于链表的长度,则该节点将附加到链表的末尾。如果 index
大于链表长度,则不会插入节点。如果index
小于0,则在头部插入节点。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]
之内。
-
- typedef struct MyLinkedList {
- int val;
- struct MyLinkedList *next;
- } MyLinkedList;
-
-
- /** Initialize your data structure here. */
- MyLinkedList* myLinkedListCreate() {
- MyLinkedList *my_linked_list = (MyLinkedList*)malloc(sizeof(MyLinkedList));
- my_linked_list->next = NULL;
- return my_linked_list;
- }
-
- /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
- int myLinkedListGet(MyLinkedList* obj, int index) {
- int i = 0;
- MyLinkedList *p;
- p = obj->next;
- if (index < 0)
- return -1;
- while (p && i < index)
- {
- p = p->next;
- i++;
- }
- if (!p || i > index)
- return -1;
- 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 myLinkedListAddAtHead(MyLinkedList* obj, int val) {
- MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
- q->next = obj->next;
- q->val = val;
- obj->next = q;
- }
-
- /** Append a node of value val to the last element of the linked list. */
- void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
- MyLinkedList *p;
- MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
- p = obj;
- while (p->next)
- {
- p = p->next;
- }
- p->next = q;
- q->val = val;
- q->next = NULL;
- }
-
- /** 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 myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
- MyLinkedList *p ;
- MyLinkedList *q = (MyLinkedList*)malloc(sizeof(MyLinkedList));
- int i = 0;
- if (index <= 0)
- {
- q->next = obj->next;
- q->val = val;
- obj->next = q;
- }
- else
- {
- p = obj;
- while (p && i < index)
- {
- p = p->next;
- i++;
- }
- if (!p || i > index)
- return;
- q->next = p->next;
- p->next = q;
- q->val = val;
- }
- }
-
- /** Delete the index-th node in the linked list, if the index is valid. */
- void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
- int i = 0;
- MyLinkedList *p, *q;
- p = obj;
- if (index < 0)
- return;
- while (p->next && i < index)
- {
- p = p->next;
- i++;
- }
- if (!p->next || i > index)
- return;
- q = p->next;
- p->next = q->next;
- free(q);
- }
-
- void myLinkedListFree(MyLinkedList* obj) {
- MyLinkedList *p, *q;
- p = obj->next;
- while (p)
- {
- q = p->next;
- free(p);
- p = q;
- }
- obj->next = NULL;
- }
-
- /**
- * Your MyLinkedList struct will be instantiated and called as such:
- * MyLinkedList* obj = myLinkedListCreate();
- * int param_1 = myLinkedListGet(obj, index);
-
- * myLinkedListAddAtHead(obj, val);
-
- * myLinkedListAddAtTail(obj, val);
-
- * myLinkedListAddAtIndex(obj, index, val);
-
- * myLinkedListDeleteAtIndex(obj, index);
-
- * myLinkedListFree(obj);
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。