算法刷题 DAY3

移除链表元素

//利用虚拟头结点统一删除头结点和非头结点的操作
//不要用头结点来遍历链表,不然最后无法返回头结点→定义一个临时指针来遍历
//cur指向dummy_head而不是dummy_head->next 因为删除结点要知道前驱

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    

    struct ListNode  * dummy_head=(struct ListNode *)malloc(sizeof(struct ListNode ));
    dummy_head->next=head;
    struct ListNode  * cur=(struct ListNode *)malloc(sizeof(struct ListNode ));
    cur=dummy_head;

    while(cur->next){
        if(cur->next->val==val){            
            cur->next= cur->next->next;
        }
        else{
            cur=cur->next;
        }
    }

return dummy_head->next;
}

707.设计链表

//第0个结点:头结点

typedef struct MyLinkedList { //括号内要用到MyLinkedList,则要在此处先命名
    int val;
    int size;//用于判断下标是否合法
    struct MyLinkedList* next;
} MyLinkedList;

MyLinkedList* myLinkedListCreate() {
    MyLinkedList* p = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    //这个题必须用虚拟头指针,参数都是一级指针,头节点确定后没法改指向了!!!
    p->next = NULL;
    p->size = 0;

    return p;
}

int myLinkedListGet(MyLinkedList* obj, int index) {
    printf("%d\n", obj->size);//用于调试
    if (index < 0 || index > obj->size - 1)
        return -1; //判断下标是否合法
    int n = index;
    MyLinkedList* cur = obj->next;
    while (n) { //利用边界条件判断用大小等于号:当n=0,不用进入循环→循环条件:n>0
        cur = cur->next;
        n--;
    }
    return cur->val; //要判断是否为空指针
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    obj->size++; //规范:每次申请一个结点,先size++
     printf("%d %d\n", val,obj->size);//用于调试
    MyLinkedList* p = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    p->val = val;
    p->next = obj->next;
    obj->next = p;
    //这个题必须用虚拟头指针,参数都是一级指针,头节点确定后没法改指向了!!
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    obj->size++;
    printf("%d %d\n", val,obj->size);//用于调试
    MyLinkedList* p = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    p->val = val;
    p->next = NULL;

    MyLinkedList* cur = obj;//尾插
    while (cur->next) {
        cur = cur->next;
    }
    cur->next = p;
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    if (index <= obj->size && index >= 0) { //下标范围两头都要考虑;此处index可以等于size(在之前没有的结点)
        MyLinkedList* cur = obj;
        int n = index;
        while (n) {//利用边界条件判断用大小等于号:当n=0,不用进入循环→循环条件:n>0
            cur = cur->next;
            n--;
        }
        obj->size++;
          printf("%d %d\n", val,obj->size);//用于调试
        MyLinkedList* p = (MyLinkedList*)malloc(sizeof(MyLinkedList));
        p->val = val;
        p->next = cur->next;
        cur->next = p;
    }
}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    //删第n个结点/在n个结点前插入结点时,cur指向第n-1个结点(要知道前驱才能进行这些操作)
    if (index < obj->size && index >= 0) {
        MyLinkedList* cur = obj;
        int n = index;
        while (n) {
            cur = cur->next;
            n--;
        }
         printf("%d\n", obj->size);//用于调试
        obj->size--; //规范:删结点前改size
         printf("%d\n", obj->size);
        cur->next = cur->next->next;
    }
}

void myLinkedListFree(MyLinkedList* obj) { //释放链表所有结点
    MyLinkedList* cur = obj;
    while (cur) {
        MyLinkedList* temp = cur->next;
        free(cur);
        cur = temp;
    }
}

/**
 * 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);
*/

/**
 * 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);
*/

/**
 * 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);
*/

反转链表

//→变←

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode * R(struct ListNode *pre,struct ListNode *cur){//没在typedef时起名→要加struct;声明时括号内莫忘加变量类型
    if(!cur) return pre;//cur为空时,pre指向原链表最后一个结点即新链表第一个结点

    struct ListNode *temp=cur->next;//当需要改变cur->next指向时(cur为工作指针),设置一个temp防止断链
    cur->next=pre;//反转链表
   return R(cur,temp);//要写return
}

struct ListNode* reverseList(struct ListNode* head) {
    return R(NULL,head);//表尾指向空结点
}

你可能感兴趣的:(算法,c语言)