删节点,
题目给的条件和给的类的参数不太明白。。。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* head,int node_val) {
if(NULL==head)return;
if(node_val==head->val){
free(head);
return;
}
ListNode *Ppre=NULL,*Pnext=NULL,*Pcur=NULL;
Ppre=head;
Pcur=head;
Pnext=Pcur->next;
while(NULL!=Pnext->next)
{
if(node_val==Pnext->val){
Ppre->next=Pnext->next;
free(Pnext);
return;
}
Pcur=Pcur->next;
Pnext=Pnext->next;
}
}
};
醉了,条件明显不足。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode *Pre=NULL,*Next=NULL,*Cur=NULL;
Cur=head;
Next=Cur->next;
while(NULL!=Next)
{
if(node==Next)
{
Pre->next=Next->next;
free(Next);
return;
}
Pre=Cur;
Next=Cur->next;
}
}
};
放弃,看大佬讨论