zoukankan      html  css  js  c++  java
  • 707. 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.

      为了增加代码复用和减少代码量,添加多一个LNode的结构体做链表结点,并把原本的结构体修改成双指针分别指向头尾和一个size属性表示链表大小

        1 typedef struct LNode{
        2     int val;
        3     //next指针用来指向链表的下一个节点,该节点同样为一个LNode结构体,因此next要声明为指向LNode结构体的指针struct LNode*。
        4     struct LNode * next;
        5 }Node;
        6 
        7 Node * make_node(int val, Node * next) {
        8     Node * rv = (Node *)malloc(sizeof(Node));  
        9     assert(rv);
       10     rv->val = val;  
       11     rv->next = next;
       12     return rv;
       13 }
       14 
       15 void free_list(Node * head) {
       16     Node * prev;
       17     while (head) {
       18             prev = head;
       19             head = head->next;
       20             free(prev);
       21     }
       22 }
       23 typedef struct {
       24     struct LNode * head;//head指针用来指向链表的头节点,该节点同样为一个LNode结构体,因此head要声明为指向LNode结构体的指针struct LNode*。
       25     struct LNode * tail;//tail指针用来指向链表的尾节点,该节点同样为一个LNode结构体,因此tail要声明为指向LNode结构体的指针struct LNode*。
       26     int size;
       27 } MyLinkedList;
       28 
       29 /** Initialize your data structure here. */
       30 MyLinkedList * myLinkedListCreate() {
       31     MyLinkedList * rv = (MyLinkedList *)calloc(1,sizeof(MyLinkedList));
       32     return rv;
       33 }
       34 
       35 /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
       36 int myLinkedListGet(MyLinkedList* obj, int index) {
       37     if(obj->size <= index)  return -1;
       38     Node * cur = obj->head;
       39     for(int i=0;i<index;i++)
       40         cur = cur->next;
       41     return cur->val;
       42 }
       43 
       44 /** 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. */
       45 void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
       46     Node * newNode = make_node(val,obj->head);
       47     obj->size += 1;
       48     obj->head = newNode;
       49     if(obj->size == 1)
       50         obj->tail = obj->head;
       51 }
       52 
       53 /** Append a node of value val to the last element of the linked list. */
       54 void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
       55     if(obj->tail) {
       56         Node * newNode = make_node(val,NULL);
       57         obj->tail->next = newNode;
       58         obj->tail = newNode;
       59         obj->size += 1;
       60     }else
       61         myLinkedListAddAtHead(obj,val);
       62 }
       63 
       64 /** 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. */
       65 void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
       66     if(obj->size < index || index < 0)   return;
       67     if(obj->size == index)
       68         return myLinkedListAddAtTail(obj,val);
       69     if(index == 0)
       70         return myLinkedListAddAtHead(obj,val);
       71     if(obj->size > index){
       72         Node * cur = obj->head;
       73         for(int i=1;i<index;i++)
       74             cur = cur->next;
       75         Node * newNode = make_node(val,cur->next);
       76         cur->next = newNode;
       77         obj->size += 1;
       78     }
       79 }
       80 
       81 /** Delete the index-th node in the linked list, if the index is valid. */
       82 void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
       83     if(obj->size <= index || index < 0)   return;
       84     if(index == 0){
       85         Node * delNode;
       86         delNode = obj->head;
       87         obj->head = delNode->next;
       88         free(delNode);
       89         obj->size -= 1;
       90         return;
       91     }
       92     if(index == obj->size-1){
       93         Node * delNode = obj->head;
       94         while(delNode->next != obj->tail)
       95             delNode = delNode->next;
       96         free(delNode->next);
       97         obj->tail = delNode;
       98         obj->size -= 1;
       99         return;
      100     }
      101     Node * delNode,* moveNode;
      102     moveNode = obj->head;
      103     for (int i = 1; i < index; i++) {
      104             moveNode = moveNode->next;
      105     }
      106     delNode = moveNode->next;
      107     moveNode->next = delNode->next;
      108     free(delNode);
      109     obj->size -= 1;
      110 }
      111 
      112 void myLinkedListFree(MyLinkedList* obj) {
      113     free_list(obj->head);
      114     free(obj);
      115 }
      116 
      117 /**
      118  * Your MyLinkedList struct will be instantiated and called as such:
      119  * struct MyLinkedList* obj = myLinkedListCreate();
      120  * int param_1 = myLinkedListGet(obj, index);
      121  * myLinkedListAddAtHead(obj, val);
      122  * myLinkedListAddAtTail(obj, val);
      123  * myLinkedListAddAtIndex(obj, index, val);
      124  * myLinkedListDeleteAtIndex(obj, index);
      125  * myLinkedListFree(obj);
      126  */


  • 相关阅读:
    第二章 算法基础 思考题2-4(逆序对)
    JSF中使用f:ajax标签无刷新页面改变数据
    JSF在ui:include中传递参数到对应控制层
    JSF通过超链接传递参数到控制层
    给JavaScript文件传入参数的几种方法
    Spring 与 @Resource注解
    GWT嵌入纯HTML页面
    一个分类,两个问题之ArrayList
    GWT更改元素样式属性
    Hello 2019 D 素因子贡献法计算期望 + 概率dp + 滚动数组
  • 原文地址:https://www.cnblogs.com/real1587/p/9932497.html
Copyright © 2011-2022 走看看