[leetcode-链表]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.

设计你的链表实现。您可以选择使用单链表或双链表。单链表中的一个节点应该有两个属性:Val*和Next.Val*是当前节点的值,而Next是下一个节点的一个指针/引用。如果你想使用双链表,你需要一个更多的属性(Prev)来指示链表中的前一个节点。假设链表中的所有节点都是0索引的。

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.

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

get(index) :获取链表中第i个索引Th节点的值。如果索引无效,返回-- 1。

addAtHead(val) :在链表的第一个元素之前添加一个值Valv。插入后,新节点将是链表的第一个节点。

addAtTail(val) :将值Valv的节点追加到链表的最后一个元素。

addAtIndex(index, val):在链表中的第k个索引节点之前加上一个值valv的节点。如果索引i等于链表的长度,则该节点将被追加到链表的末尾。如果索引大于长度,则节点将不被插入。

deleteAtIndex(index) :如果索引有效,则删除链表中的第k个索引节点。

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.

//================================================================================
 

struct Node {
    int val;
    struct Node *next;
}; 
typedef struct {
    struct Node *head;
} MyLinkedList;

/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
    MyLinkedList *node = (MyLinkedList *)malloc(sizeof(MyLinkedList));
    node->head = NULL;
    return node;
}

/** 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) {
    if(obj==NULL || obj->head==NULL)
    {
        return -1;
    }
    struct Node*p=obj->head;
    while (p && (index>0))
    {
        p=p->next;
        index--;
    }
    if(p && (0 == index))
    {
        return p->val;
    }
    else
    {
        return -1;
    }
}

/** 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) {
    if(obj==NULL)
    {
        return;
    }
    
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    if(node == NULL)
    {
        return;
    }

    
    node->val = val;
    node->next=NULL;
    if(obj->head!=NULL)
    {
        node->next=obj->head;
    }
    obj->head=node;
}

/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    if(obj==NULL)
    {
        return;
    }
    
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    if(node == NULL)
    {
        return;
    }
    node->val=val;
    node->next=NULL;
    
    struct Node *p=obj->head;
    while(p && p->next)
    {
        p=p->next;
    }
    p->next=node;
    
    return;
}

/** 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) {
    if(obj==NULL)
    {
        return;
    }
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    if(node == NULL)
    {
        return;
    }
    node->val=val;
    node->next=NULL;
    
    struct Node*p=obj->head;
    if(index==0)
    {
        node->next=p;
        obj->head=node;
        return;
    }
    
    struct Node *prev=p;
    while(p && (index>0))
    {
        prev=p;
        p=p->next;
        index--;
    }
    
    if(0 == index)
    {
        node->next = prev->next;
        prev->next=node;
    }
}

/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    if(obj==NULL)
    {
        return;
    }
    struct Node *p=obj->head;
    if(index==0)
    {
        obj->head=p->next;
        free(p);
        return; 
    }
    
    struct Node *prev;
    while(p && (index>0))
    {
        prev=p;
        p=p->next;
        index--;
    }
    
    if(0 == index)
    {
        if(p)
        {
            prev->next=p->next;
            if(p)
                free(p);
        }

    }   
}

void myLinkedListFree(MyLinkedList* obj) {
    if(obj==NULL)
    {
        return;
    }
    
    struct Node *p=obj->head;
    struct Node *q=NULL;
    while(p)
    {
        q=p;
        p=p->next;
        free(p);
    }
    obj->head=NULL;
}

/**
 * Your MyLinkedList struct will be instantiated and called as such:
 * struct MyLinkedList* obj = myLinkedListCreate();
 * int param_1 = myLinkedListGet(obj, index);
 * myLinkedListAddAtHead(obj, val);
 * myLinkedListAddAtTail(obj, val);
 * myLinkedListAddAtIndex(obj, index, val);
 * myLinkedListDeleteAtIndex(obj, index);
 * myLinkedListFree(obj);
 */

猜你喜欢

转载自blog.csdn.net/qq_20398345/article/details/81093794