【每日刷题】Day30
🥕个人主页:开敲🍉
🔥所属专栏:每日刷题🍍
🌼文章目录🌼
1. 牛牛的链表添加节点_牛客题霸_牛客网 (nowcoder.com)
2. 牛牛的链表删除_牛客题霸_牛客网 (nowcoder.com)
3. 牛牛的双链表求和_牛客题霸_牛客网 (nowcoder.com)
1. 牛牛的链表添加节点_牛客题霸_牛客网 (nowcoder.com)
//思路:无。链表基础
#include <stdio.h>
typedef struct ListNode
{
int val;
struct ListNode* next;
}LN;
int main()
{
int n = 0;
int x = 0;
scanf("%d",&n);
scanf("%d",&x);
int y = x;
int arr[10000] = {0};
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
LN* Sentry = (LN*)malloc(sizeof(LN));
LN* newhead = Sentry;
int j = 0;
for(;j<i;j++)
{
LN* newnode = (LN*)malloc(sizeof(LN));
newnode->next = NULL;
newnode->val = arr[j];
newhead->next = newnode;
newhead = newhead->next;
}
LN* pmove = Sentry->next;
while(--y)
{
pmove = pmove->next;
}
LN* newnode = (LN*)malloc(sizeof(LN));
newnode->val = x;
newnode->next = pmove->next;
pmove->next = newnode;
LN* pprint = Sentry->next;
while(pprint)
{
printf("%d ",pprint->val);
pprint = pprint->next;
}
return 0;
}
2. 牛牛的链表删除_牛客题霸_牛客网 (nowcoder.com)
//思路:三指针。一个指针指向需要删除节点的上一个节点,一个指针指向需要删除节点的下一个节点。特别需要注意每个指针行动的时机。
#include <stdio.h>
typedef struct ListNode
{
int val;
struct ListNode* next;
}LN;
int main()
{
int n = 0;
int x = 0;
scanf("%d",&n);
scanf("%d",&x);
int arr[10000] = {0};
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
LN* Sentry = (LN*)malloc(sizeof(LN));
LN* newhead = Sentry;
int j = 0;
for(;j<i;j++)//将数组中的数据存入链表
{
LN* newnode = (LN*)malloc(sizeof(LN));
newnode->next = NULL;
newnode->val = arr[j];
newhead->next = newnode;
newhead = newhead->next;
}
LN* pdel = Sentry->next;
LN* pcur = Sentry;
while(pdel->next)//用pdel->next是为了防止下面对tmp空指针的解引用
{
LN* tmp = pdel->next;
if(pdel->val==x)//如果pdel指针指向的是需要删除的节点,pcur->next指向要删除节点的下一个节点
{
pcur->next = pdel->next;
free(pdel);//释放要要删除的节点
pdel = tmp;//走向下一个位置
}
else//如果pdel指向的不是需要删除的节点,pcur和pdel都往前走
{
pcur = pcur->next;
pdel = tmp;
}
}
if(pdel->val==x)//由于上面while的条件是pdel->next,因此最后一个节点没法处理,这里出了循环需要再处理一下
{
pcur->next = pdel->next;
free(pdel);
pdel = NULL;
}
LN* pmove = Sentry->next;
while(pmove)
{
printf("%d ",pmove->val);
pmove = pmove->next;
}
return 0;
}
3. 牛牛的双链表求和_牛客题霸_牛客网 (nowcoder.com)
//思路:双指针。链表基础。
#include <stdio.h>
typedef struct ListNode
{
int val;
struct ListNode* next;
}LN;
int main()
{
int n = 0;
scanf("%d",&n);
int arr1[10000] = {0};
int arr2[10000] = {0};
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
for(i = 0;i<n;i++)
{
scanf("%d",&arr2[i]);
}
LN* Sentry1 = (LN*)malloc(sizeof(LN));
LN* Sentry2 = (LN*)malloc(sizeof(LN));
LN* newhead1 = Sentry1;
LN* newhead2 = Sentry2;
int j = 0;
for(;j<i;j++)
{
LN* newnode1 = (LN*)malloc(sizeof(LN));
LN* newnode2 = (LN*)malloc(sizeof(LN));
newnode1->next = NULL;
newnode2->next = NULL;
newnode1->val = arr1[j];
newnode2->val = arr2[j];
newhead1->next = newnode1;
newhead2->next = newnode2;
newhead1 = newhead1->next;
newhead2 = newhead2->next;
}
LN* pmove1 = Sentry1->next;
LN* pmove2 = Sentry2->next;
while(pmove1)
{
pmove2->val+=pmove1->val;
pmove1 = pmove1->next;
pmove2 = pmove2->next;
}
LN* pmove = Sentry2->next;
while(pmove)
{
printf("%d ",pmove->val);
pmove = pmove->next;
}
return 0;
}