代码随想录第三天(一刷&&C语言)|虚拟头节点&&反转链表

创作目的:为了方便自己后续复习重点,以及养成写博客的习惯。

一、虚拟头节点

1、增加虚拟头结点可以统一元素的操作。

2、避免了将之前头结点单独拿出来处理和判断的步骤。

3、留意cur与cur->next作为循环的条件,进行终止与变化。

(一)移除链表元素

ledcode 题目:

AC代码1(不使用虚拟头结点):

AC代码2(使用虚拟头结点):(二)设计链表

题目无必要

写的代码AC不了,难为以后的自己吧




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

MyLinkedList* myLinkedListCreate() {
    MyLinkedList* mylinklist = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    mylinklist->val = 0;
    mylinklist->next = NULL;
    return mylinklist;  
}

int myLinkedListGet(MyLinkedList* obj, int index) {
    MyLinkedList* cur = obj->next;
    int i = 0;
    while(cur&&i++) {
        if(i == index) {
            return cur->val;
        }else {
            cur = cur->next;
        }
    }
    return -1;
}

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
    MyLinkedList* hnode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    hnode->val = val;
    hnode->next = obj->next;
    obj->next = hnode;
}

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    MyLinkedList* tnode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
    MyLinkedList* cur = obj;
    while(cur->next) {
        cur = cur->next;
    }
    tnode->val = val;
    tnode->next = NULL;
    cur->next = tnode;
}

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
    if(index == 0) {
        myLinkedListAddAtHead(obj,val);
        return;
    }
    MyLinkedList* cur = obj->next;
    for(int i = 1;cur;i++) {
        if(i == index) {
             MyLinkedList* node = (MyLinkedList*)malloc(sizeof(MyLinkedList));
             node->val = val;
             node->next = cur->next;
             cur->next = node;
             return;
        }else {
            cur = cur->next;
        }
    }

}

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    if(index == 0) {
        MyLinkedList *tmp = obj->next;
        if(tmp){
            obj->next = tmp->next;
            free(tmp);
        }
        return;
    }
    MyLinkedList* cur = obj->next;
    int i = 1;
    while(cur->next && cur && i++) {
        if(i == index) {
            MyLinkedList *tmp = cur->next;
            if(tmp) {
                cur->next = tmp->next;
                free(tmp);
            }                       
        }else {
            cur = cur->next;
        }
    }
}

void myLinkedListFree(MyLinkedList* obj) {
    while(obj) {
        MyLinkedList *tmp = obj;
        obj = obj->next;
        free(tmp);
    }
}

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

直接复制carl的代码,我直接AC好吧,ac通过了,很开心。

AC代码:

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

/** Initialize your data structure here. */

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

/** 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) {
    MyLinkedList *cur = obj->next;
    for (int i = 0; cur != NULL; i++){
        if (i == index){
            return cur->val;
        }
        else{
            cur = cur->next;
        }
    }
    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) {
    MyLinkedList *nhead = (MyLinkedList *)malloc(sizeof (MyLinkedList));
    nhead->val = val;
    nhead->next = obj->next;
    obj->next = nhead;

}

/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
    MyLinkedList *cur = obj;
    while(cur->next != NULL){
        cur = cur->next;
    }
    MyLinkedList *ntail = (MyLinkedList *)malloc(sizeof (MyLinkedList));
    ntail->val = val;
    ntail->next = NULL;
    cur->next = ntail;
}

/** 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 (index == 0){
        myLinkedListAddAtHead(obj, val);
        return;
    }
    MyLinkedList *cur = obj->next;
    for (int i = 1 ;cur != NULL; i++){
        if (i == index){
            MyLinkedList* newnode = (MyLinkedList *)malloc(sizeof (MyLinkedList));
            newnode->val = val;
            newnode->next = cur->next;
            cur->next = newnode;
            return;
        }
        else{
            cur = cur->next;
        }
    }
}

/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
    if (index == 0){
        MyLinkedList *tmp = obj->next;
        if (tmp != NULL){
            obj->next = tmp->next;
            free(tmp);     
        }
        return;
    }
    MyLinkedList *cur = obj->next;
    for (int i = 1 ;cur != NULL && cur->next != NULL; i++){
        if (i == index){
            MyLinkedList *tmp = cur->next;
            if (tmp != NULL) {
                cur->next = tmp->next;
                free(tmp);
            }
            return;
        }
        else{
            cur = cur->next;
        }
    }
    
}

void myLinkedListFree(MyLinkedList* obj) {
    while(obj != NULL){
        MyLinkedList *tmp = obj;
        obj = obj->next;
        free(tmp);
    }
}

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

二、反转链表

1、要手动模拟出其过程

2、设置好指针指向

3、循环条件考虑,以及反转的操作

ledcode题目:

AC代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* temp;
    struct ListNode* pre = NULL;
    struct ListNode* cur = head;
    while(cur) {
        temp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = temp;
    }
    return pre;
}

题外:随心去学,坚持很重要。博客创作的操作似乎也在慢慢的熟练了。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值