习题11-7 奇数值结点链表
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
函数接口定义:
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
函数readlist
从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数getodd
将单链表L
中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L
中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L
的指针)。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *L, *Odd;
L = readlist();
Odd = getodd(&L);
printlist(Odd);
printlist(L);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
1 2 2 3 4 5 6 7 -1
输出样例:
1 3 5 7
2 2 4 6
解题思路:链表常规题目。
struct ListNode *readlist() {
struct ListNode *head = NULL, *prev = NULL, *curr = NULL;
int val;
scanf("%d", &val);
while (val != -1) {
curr = (struct ListNode *)malloc(sizeof(struct ListNode));
curr->data = val;
curr->next = NULL;
if (NULL == head) head = curr; // 1
else prev->next = curr; // 3
prev = curr; // 2
scanf("%d", &val);
}
return head;
}
struct ListNode *getodd(struct ListNode **L) {
// 题意传指针就是可能首节点是 odd,要求返回参数和修改的 L 一模一样,因此调用两次
struct ListNode *dummy1 = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy1->next = *L; // 作为头结点
struct ListNode *prev = dummy1, *curr = *L, *nxt = NULL;
struct ListNode *dummy2 = (struct ListNode *)calloc(1, sizeof(struct ListNode)), *front = dummy2; // dummy2->next=NULL
while (curr != NULL) {
// 1.如果是奇数,那么prev任然是前驱,保留nxt=curr->next,删除此时curr,然后curr=nxt继续遍历
if (curr->data & 1) {
nxt = curr->next;
prev->next = nxt;
front->next = curr; // odd表连接curr
curr->next = NULL; // 从上面取下来,不能拉着后代
front = curr; // 移动
curr = nxt;
} else { // 2.不删除,更新前驱和继续遍历
prev = curr;
curr = curr->next;
}
}
*L = dummy1->next; // 修改原链表
free(dummy1); // 可选删除释放哨兵节点
front = dummy2->next;
free(dummy2); // 可选删除释放哨兵节点
return front;
}
Case | Hint | Result | Score | Run Time | Memory |
---|---|---|---|---|---|
0 | sample等价,头尾结点都是奇数 | Accepted | 10 | 3 ms | 368 KB |
1 | 头尾结点都是偶数 | Accepted | 2 | 2 ms | 256 KB |
2 | 全是奇数 | Accepted | 2 | 3 ms | 360 KB |
3 | 全是偶数 | Accepted | 2 | 2 ms | 256 KB |
4 | 只有一个奇数结点 | Accepted | 2 | 2 ms | 256 KB |
5 | 只有一个偶数结点 | Accepted | 2 | 2 ms | 256 KB |
习题11-8 单链表结点删除
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
函数接口定义:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
函数readlist
从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deletem
将单链表L
中所有存储了m
的结点删除。返回指向结果链表头结点的指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int m;
struct ListNode *L = readlist();
scanf("%d", &m);
L = deletem(L, m);
printlist(L);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10 11 10 12 10 -1
10
输出样例:
11 12
解题思路:单链表的删除只需要找到待删除链表的前驱节点即可(头节点单独处理)
struct ListNode *readlist() {
struct ListNode *head = NULL, *prev = NULL, *curr = NULL;
int val;
scanf("%d", &val);
while (val != -1) {
curr = (struct ListNode *)malloc(sizeof(struct ListNode));
curr->data = val;
curr->next = NULL;
if (NULL == head) head = curr; // 1
else prev->next = curr; // 3
prev = curr; // 2
scanf("%d", &val);
}
return head;
}
struct ListNode *deletem(struct ListNode *L, int m) {
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->next = L; // 作为头结点
struct ListNode *prev = dummy, *curr = L, *nxt = NULL;
while (curr != NULL) {
// 1.如果curr->data=m,prev还是前驱,保留nxt=curr->next,删除此时curr,然后curr=nxt继续遍历
if (curr->data == m) {
nxt = curr->next;
prev->next = nxt;
free(curr); // 可选删除
curr = nxt;
} else { // 2.不删除,更新前驱和继续遍历
prev = curr;
curr = curr->next;
}
}
curr = dummy->next;
free(dummy); // 可选删除释放哨兵节点
return curr;
}
Case | Hint | Result | Score | Run Time | Memory |
---|---|---|---|---|---|
0 | sample等价,从头、中、尾删除 | Accepted | 14 | 2 ms | 296 KB |
1 | 全部删除 | Accepted | 2 | 2 ms | 268 KB |
2 | 没有删除 | Accepted | 2 | 2 ms | 256 KB |
3 | 连续从头、中、尾删除 | Accepted | 2 | 2 ms | 384 KB |