题解 | #在链表中删除指定值的节点#
在链表中删除指定值的节点
http://www.nowcoder.com/practice/1a5fd679e31f4145a10d46bb8fd3d211
# include <bits/stdc++.h>
using namespace std;
struct list_node{
int val;
struct list_node * next;
};
list_node * input_list()
{
int val, n;
scanf("%d", &n);
list_node * phead = new list_node();
list_node * cur_pnode = phead;
for (int i = 1; i <= n; ++i) {
scanf("%d", &val);
if (i == 1) {
cur_pnode->val = val;
cur_pnode->next = NULL;
}
else {
list_node * new_pnode = new list_node();
new_pnode->val = val;
new_pnode->next = NULL;
cur_pnode->next = new_pnode;
cur_pnode = new_pnode;
}
}
return phead;
}
list_node * remove_value(list_node * head, int num)
{
//////在下面完成代码
//注意链表中可能含有多个节点值为num的节点,都要删除
list_node*pre=NULL,*cur=head;
while(cur)
{
if(cur->val==num){
if(pre==NULL)
{
head=head->next;
cur=head;
}
else{
pre->next=cur->next;
cur=pre->next;
}
}
else{
pre=cur;
cur=cur->next;
}
}
return head;
}
void print_list(list_node * head)
{
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
puts("");
}
int main ()
{
list_node * head = input_list();
int num;
scanf("%d", &num);
list_node * new_head = remove_value(head, num);
print_list(new_head);
return 0;
}
using namespace std;
struct list_node{
int val;
struct list_node * next;
};
list_node * input_list()
{
int val, n;
scanf("%d", &n);
list_node * phead = new list_node();
list_node * cur_pnode = phead;
for (int i = 1; i <= n; ++i) {
scanf("%d", &val);
if (i == 1) {
cur_pnode->val = val;
cur_pnode->next = NULL;
}
else {
list_node * new_pnode = new list_node();
new_pnode->val = val;
new_pnode->next = NULL;
cur_pnode->next = new_pnode;
cur_pnode = new_pnode;
}
}
return phead;
}
list_node * remove_value(list_node * head, int num)
{
//////在下面完成代码
//注意链表中可能含有多个节点值为num的节点,都要删除
list_node*pre=NULL,*cur=head;
while(cur)
{
if(cur->val==num){
if(pre==NULL)
{
head=head->next;
cur=head;
}
else{
pre->next=cur->next;
cur=pre->next;
}
}
else{
pre=cur;
cur=cur->next;
}
}
return head;
}
void print_list(list_node * head)
{
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
puts("");
}
int main ()
{
list_node * head = input_list();
int num;
scanf("%d", &num);
list_node * new_head = remove_value(head, num);
print_list(new_head);
return 0;
}