LeetCode/NowCoder-链表经典算法OJ练习1

目录

说在前面

题目一:移除链表元素

题目二:反转链表

题目三:合并两个有序链表

题目四:链表的中间节点

SUMUP结尾


说在前面

 dear朋友们大家好!💖💖💖数据结构的学习离不开刷题,之前给大家推荐过两个网站-牛客网和力扣,一个好的刷题网站是非常重要的,数据结构的思维需要靠刷题培养。我们上个篇目给大家介绍了单链表,也带大家实现了单链表的基本操作,我们今天趁热打铁,在leetcode上找一些经典例题给大家看看

 👇👇👇

友友们!🎉🎉🎉点击这里进入力扣leetcode学习🎉🎉🎉
​以下是leetcode题库界面:

 

题目一:移除链表元素

题目链接:203. 移除链表元素 - 力扣(LeetCode)

题目描述:

 题目分析:

思路1:遍历原链表,将值为val的节点释放掉。

 代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)//如果链表为空,返回NULL
        return NULL;
    //设置前后指针
    ListNode* pcur = head;
    ListNode* prev = NULL;
    while (pcur)
    {
        if (pcur->val == val)
        {
            ListNode* del = pcur;
            if (prev == NULL)//如果第一个值就是val
            {
                pcur = del->next;
                head = pcur;//重置头节点
            }
            else
            {
                prev->next = pcur->next;
                pcur = pcur->next;
            }
            free(del);
        }
        //前后指针前进
        else
        {
            prev = pcur;
            pcur = pcur->next;
        }
    }
    return head;
}

思路2:创建新链表,找值不为val的节点,尾插到新链表中。

代码如下: 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
    //创建新链表
    ListNode* newhead = NULL;
    ListNode* newtail = NULL;
    ListNode* pcur = head;
    while (pcur)
    {
        //如果值不为val,插入新链表
        if (pcur->val != val)
        {
            //分为newhead为NULL和不为NULL两种情况
            if (newhead == NULL)
                newhead = newtail = pcur;
            else
            {
                newtail->next = pcur;
                newtail = pcur;
            }
        }
        pcur = pcur->next;
    }
    if (newtail)//保证next指针可访问
        newtail->next = NULL;
    return newhead;
}

 

题目二:反转链表

题目链接:206. 反转链表 - 力扣(LeetCode)

题目描述:

题目分析:

思路1:创建新链表,将原链表的节点进行头插。

代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head) 
{
    //创建新链表
    ListNode* newhead = NULL;
    ListNode* pcur = head;
    while (pcur)
    {
        //为了让pcur能找到原链表的下一个节点,创建临时变量temp
        ListNode* temp = pcur->next;
        pcur->next = newhead;
        newhead = pcur;
        pcur = temp;
    }
    return newhead;
}
#endif

思路2:创建三个指针n1、n2、n3,完成原链表的反转。

代码如下: 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head) {
    //使用head->next,需要保证head不为空,单独讨论
    if (head == NULL)
        return NULL;
    //创建n1、n2、n3三个指针
    ListNode* n1 = NULL;
    ListNode* n2 = head;
    ListNode* n3 = head->next;
    while (n2)
    {
        //反转链表
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if (n3)//使用n3->next,则n3不为空
            n3 = n3->next;
    }
    return n1;
}

 

题目三:合并两个有序链表

题目链接:21. 合并两个有序链表 - 力扣(LeetCode)

题目描述:

题目分析: 

思路:创建新的空链表,遍历原链表,将节点值小的节点拿到新链表中进行尾插操作。

写法1:不带哨兵位节点创建新链表。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{
    //其中有一个空链表,就直接返回另一个
    if(list1==NULL)
    return list2;
    if(list2==NULL)
    return list1;
    ListNode* n1 = list1;
    ListNode* n2 = list2;
    //创建新链表
    ListNode* newhead = NULL;
    ListNode* newtail = NULL;
    while (n1 && n2)//两者都不为空则进入循环
    {
        //谁的值小,则先将该节点尾插到新链表
        if (n1->val < n2->val)
        {
            if (newhead == NULL)
                newhead = newtail = n1;
            else
            {
                newtail->next = n1;
                newtail = n1;
            }
            n1 = n1->next;
        }
        else
        {
            if (newhead == NULL)
                newhead = newtail = n2;
            else
            {
                newtail->next = n2;
                newtail = n2;
            }
            n2 = n2->next;
        }
    }
    //出循环是因为n2尾插完,则把n1剩下的节点直接尾插接口
    while (n1)
    {
        newtail->next = n1;
        newtail = n1;
        n1 = n1->next;
    }
    //出循环是因为n1尾插完,则把n2剩下的节点直接尾插接口
    while (n2)
    {
        newtail->next = n2;
        newtail = n2;
        n2 = n2->next;
    }
    if (newtail)
        newtail->next = NULL;
    return newhead;
}

写法2:哨兵位节点创建新链表(推荐)。

typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    ListNode* n1 = list1;
    ListNode* n2 = list2;
    ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
    ListNode* newtail = newhead;
    while (n1 && n2)
    {
        if (n1->val < n2->val)
        {
            newtail->next = n1;
            newtail = n1;
            n1 = n1->next;
        }
        else
        {
            newtail->next = n2;
            newtail = n2;
            n2 = n2->next;
        }
    }
    while (n1)
    {
        newtail->next = n1;
        newtail = n1;
        n1 = n1->next;
    }
    while (n2)
    {
        newtail->next = n2;
        newtail = n2;
        n2 = n2->next;
    }
    if (newtail)
        newtail->next = NULL;
    ListNode* ret = newhead->next;
    free(newhead);
    return ret;
}

 

题目四:链表的中间节点

题目链接:876. 链表的中间结点 - 力扣(LeetCode)

题目描述:

题目分析:

思路1:遍历链表,利用count计数,返回(count / 2)节点的next节点。(无关节点数奇偶性,比如节点数为5,则删除5 / 2 = 2的后一个,也就是第三个节点;节点数为6,则删除6 / 2 = 3的后一个,也就是第四个节点)

代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* middleNode(struct ListNode* head) {
    ListNode* pcur = head;
    int count = 0;//count计数链表个数
    while (pcur)
    {
        pcur = pcur->next;
        count++;
    }
    pcur = head;
    int ret = count / 2;
    while (ret--)//返回第count/2节点的后一个节点
    {
        pcur = pcur->next;
    }
    return pcur;
}

思路2:快慢指针法(定义快慢指针fast、slow,slow每次走一步,fast每次走两步)。

代码如下: 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head) 
{
    //定义快慢指针
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next)//顺序不能反,当有偶数个节点时,fast直接走到NULL,fast->next报错
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    //此时fast走完,slow走了一半
    return slow;
}

 

SUMUP结尾

数据结构就像数学题,需要刷题才能对它有感觉。之后还会更新数据结构相关的练习题、面试题,希望大家一起学习,共同进步~

 

如果大家觉得有帮助,麻烦大家点点赞,如果有错误的地方也欢迎大家指出~

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 04:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 04:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 04:34:03       82 阅读
  4. Python语言-面向对象

    2024-05-12 04:34:03       91 阅读

热门阅读

  1. 项目环境准备

    2024-05-12 04:34:03       34 阅读
  2. git mv命令不会自动建立目录的坑

    2024-05-12 04:34:03       98 阅读
  3. stylus详解与引入

    2024-05-12 04:34:03       33 阅读
  4. ffmpeg常用的命令(windows)

    2024-05-12 04:34:03       34 阅读
  5. 算法精讲:选择排序

    2024-05-12 04:34:03       31 阅读
  6. 爬虫工具-推广-推广链接

    2024-05-12 04:34:03       35 阅读
  7. Python执行程序获取所在目录

    2024-05-12 04:34:03       33 阅读