代码随想录算法训练营第三天| 203.移除链表元素 707.设计链表 206.反转链表

本文回顾了三道关于链表的算法题,分别是203题移除链表元素、707题设计链表和206题反转链表。在移除元素中强调了链表基础知识的重要性;设计链表时注意了实现细节避免错误;反转链表探讨了双指针和递归两种解法。
摘要由CSDN通过智能技术生成

题目链接:203.移除链表元素

在学数据结构时,在链表头部设置一个空结点,这样原链表的所有节点就都可以按照统一的方式进行移除,这道题刚好重温一下链表的基础知识。

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


struct ListNode* removeElements(struct ListNode* head, int val){
    typedef struct ListNode ListNode;
    ListNode* shead;
    shead=(ListNode*)malloc(sizeof(ListNode));
    shead->next=head;
     ListNode *cur = shead;
    while(cur->next!=NULL)
    {
        if(cur->next->val==val)
        {
            ListNode* temp=cur->next;
            cur->next=cur->next->next;
            free(temp);
        }
        
        else
        cur=cur->next;
    }
    head=shead->next;
    free(shead);
    return head;
}

题目链接:707.设计链表

这道题目在学过链表的基础知识后,逻辑上并不算太难,但当代码真正运转起来,一直出现细微的报错,修补了很久才通过,细节问题上很容易翻车。

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


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;
}

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


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;
}

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;
        }
    }
}


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


}

void myLinkedListFree(MyLinkedList* obj) {
    while(obj!=NULL)
    {
        MyLinkedList* temp=obj;
        obj=obj->next;
        free(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);
*/

题目链接:206.反转链表

这道题逻辑不算难,可以用双指针和递归两种方法作答。

(1)双指针法:

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* pre=NULL;
    struct ListNode* temp;
    while(head)
    {
        temp=head->next;
        head->next=pre;
        pre=head;
        head=temp;
        

    }
    return pre;
}

(2)递归法:

struct ListNode* revere(struct ListNode* pre,struct ListNode* cur)
 {
     if(!cur)
     {
         return pre;
     }
     struct ListNode* temp=cur->next;
     cur->next=pre;
     return revere(cur,temp);
 }


struct ListNode* reverseList(struct ListNode* head){
    return revere(NULL,head);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值