0
点赞
收藏
分享

微信扫一扫

第十三章数据质量练习

目录

说在前面

题目一:移除链表元素

题目二:反转链表

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

题目四:链表的中间节点

SUMUP结尾


 

题目一:移除链表元素

题目链接: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;
}

 

举报

相关推荐

0 条评论