删节点,

题目给的条件和给的类的参数不太明白。。。

/**
 * 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;
        }
        
    }
};

 

 

leetcode:237. Delete Node in a Linked List

 

放弃,看大佬讨论

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2021-07-13
猜你喜欢
  • 2021-06-22
  • 2021-12-03
  • 2022-02-10
  • 2021-12-27
  • 2021-07-19
  • 2022-01-03
相关资源
相似解决方案