给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。
1.此题对比原题有改动
2.题目保证链表中节点的值互不相同
3.该题只会输出返回的链表和结果做对比,所以若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点
数据范围:
0<=链表节点值<=10000
0<=链表长度<=10000
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。
{2,5,1,9},5{2,1,9}
给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 2 -> 1 -> 9
{2,5,1,9},1{2,5,9}
给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 2 -> 5 -> 9
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
#include <stdio.h>
struct ListNode* deleteNode(struct ListNode* head, int val ) {
// write code here
struct ListNode *h,*p;
h->next=head;
p=h;
while(p->next){
if (p->next->val==val) {
break;
}
else {
p=p->next;
}
}
p->next=p->next->next;
return h->next;
} struct ListNode* deleteNode(struct ListNode* head, int val )
{
//val在头结点的情况,直接更新头结点指针
if(head->val == val)
{
head = head->next;
return head;
}
//val不在头结点的情况,定位val结点位置
struct ListNode* pcur = head;//val结点
struct ListNode* prev = head;//val结点的前驱指针
while(pcur->val != val)
{
prev = pcur;
pcur = pcur->next;
}
prev->next = pcur->next;
return head;
} struct ListNode* deleteNode(struct ListNode* head, int val ) {
// write code here
struct ListNode* pr = head;
struct ListNode* pf = head;
while (pf != NULL) {
if (pf->val == val) {
if (pf == head) head = head->next;
else pr->next = pf->next;
return head;
} else {
pr = pf;
pf = pf->next;
}
}
return head;
} struct ListNode* deleteNode(struct ListNode* head, int val )
{
struct ListNode *header = (struct ListNode* )malloc(sizeof(struct ListNode));
header->next = head;
struct ListNode *cur,*p;
cur = header;
while(cur->next->val!=val)
cur = cur->next;
p = cur->next;
cur->next = cur->next->next;
free(p);
return header->next;
} struct ListNode* deleteNode(struct ListNode* head, int val ) {
struct ListNode* L = (struct ListNode* )malloc(sizeof(struct ListNode));
L->next = head;
struct ListNode* p = head;
struct ListNode* pre = L;
while(p) {
if(p->val == val) {
p = p->next;
pre->next = p;
} else {
p = p->next;
pre = pre->next;
}
}
return L->next;
} struct ListNode* deleteNode(struct ListNode* head, int val )
{
// write code here
if(head->val==val)//特殊情况,删除头节点
{
return head->next;
}
else//设置前后指针
{
struct ListNode* prev=head;
struct ListNode* tail=head;
while(tail->val!=val)
{
prev=tail;
tail=tail->next;
}
prev->next=tail->next;
return head;
}
} struct ListNode* deleteNode(struct ListNode* head, int val ) {
if(head == NULL) return head;
if(head->val == val) return head->next;
struct ListNode *p = head,*pn = head->next;
while(pn){
if(pn->val == val){
p->next = pn->next;
free(pn);
break;
}
p = pn;
pn = pn->next;
}
return head;
// write code here
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
struct ListNode* deleteNode(struct ListNode* head, int val ) {
// write code here
//特殊情况,删除的是头节点
if(head->val==val)
return head->next;
//设置前后指针,指向头节点和下一个节点。
struct ListNode* front = head;
struct ListNode* behind = head->next;
while(behind!=NULL && behind->val!=val)
{
front=behind;
behind=behind->next;
}
front->next = behind->next;
return head;
}