Problema DO de lista enlazada

inserte la descripción de la imagen aquí
Hoy hablaré sobre algunas preguntas de Oj sobre listas enlazadas, creo que elevarás la lista enlazada a otro nivel después de leerla.

tema uno

inserte la descripción de la imagen aquí
Idea 1
Recorrer la lista enlazada es gratis cuando vale la pena, y luego retrocedemos hasta llegar al puntero nulo al final.La nueva lista enlazada es nuestra respuesta, así que expresémosla con código.


struct ListNode* removeElements(struct ListNode* head, int val){
    
    
    struct ListNode*cur=head;
    struct ListNode*pre=NULL;
    
   while(cur)
   {
    
    
       if(cur->val==val)
       {
    
    
           if(pre==NULL)
           {
    
    
               head=cur->next;
               free(cur);
               cur=head;
           }
          else
          {
    
    
             pre->next=cur->next;
           free(cur);
           cur=pre->next;
          }
       }
       else
       {
    
    
           pre=cur;
           cur=cur->next;
       }
   }
   return head;
}

Idea 2
Si no es val, lo bajaremos, si es val, lo omitiremos y lo pondremos en una nueva lista enlazada.

struct ListNode* removeElements(struct ListNode* head, int val){
    
    
        struct ListNode*tail=NULL;
        struct ListNode*cur=head;
       
        head=NULL;
        while(cur)
        {
    
    
            if(cur->val==val)
            {
    
    
                cur=cur->next;
            }
            else
            {
    
    
                if(tail==NULL)
                {
    
    
                    head=tail=cur;
                }
                else
                {
    
    
                    tail->next=cur;
                    tail=tail->next;
                }
                 cur=cur->next;
            }
        }
        if(tail)
          tail->next=NULL;   

    return head;
}

Idea 3
La forma de obtener el nodo principal con la posición centinela es eliminar el val, no omitirlo.


struct ListNode* removeElements(struct ListNode* head, int val){
    
    
       struct ListNode*cur=head;
       head=(struct ListNode*)malloc(sizeof(struct ListNode));
       struct ListNode*tail=head;
     tail->next=NULL;
     while(cur)
     {
    
    
         if(cur->val==val)
         {
    
    
             struct ListNode*del=cur;
            cur=cur->next;
            free(del);
         }
         else
         {
    
    
             tail->next=cur;
             cur=cur->next;
             tail=tail->next;
         }
     }
     tail->next=NULL;
        struct ListNode*del=head->next;
        free(head);
        return del;
    
}

tema dos

inserte la descripción de la imagen aquí

Podemos cambiar la dirección de esta pregunta, dar tres variables indicadoras y cambiar la dirección para resolverla.

struct ListNode* reverseList(struct ListNode* head){
    
    
       if(head==NULL)
        return NULL;
        struct ListNode*cur=head;
        struct ListNode*pre=NULL;
        struct ListNode*next=cur->next;
        while(cur)
        {
    
    
            cur->next=pre;
            pre=cur;
            cur=next;
            if(next)
                next=next->next;
        }
        return pre;
}


Simplemente inserte el segundo extremo de la idea en la nueva lista vinculada.

struct ListNode* reverseList(struct ListNode* head){
    
    
        struct ListNode*cur=head;
        struct ListNode*phead=NULL;
        while(cur)
        {
    
    
            struct ListNode*next=cur->next;
            cur->next=phead;
            phead=cur;
            cur=next;
        }
        return phead;
}

tema tres

inserte la descripción de la imagen aquí

Use el puntero de velocidad, dé dos pasos rápidos y uno lento, y podrá resolver el problema.

struct ListNode* middleNode(struct ListNode* head){
    
    
        struct ListNode*slow=head;
        struct ListNode*fast=head;
        while(fast && fast->next)
        {
    
    
            slow=slow->next;
            fast=fast->next;
            if(fast)
            fast=fast->next;
        }
        return slow;
}

tema cuatro

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    
    
    struct ListNode*slow=pListHead;
     struct ListNode*fast=pListHead;
     
     while((k--))
     {
    
    
        if(fast==NULL)
            return NULL;
        fast=fast->next;
     }
     while(fast)
     {
    
    
        fast=fast->next;
        slow=slow->next;
     }
     return slow;
}

inserte la descripción de la imagen aquí

Esta pregunta es similar al puntero rápido y lento. Deje que el puntero rápido dé k pasos primero y luego camine al mismo tiempo. La condición final es cuando el puntero rápido está vacío.

¡Hoy compartiré cuatro temas primero, y continuaré compartiendo algunos más más tarde! ! ! gracias por ver

Supongo que te gusta

Origin blog.csdn.net/2301_76895050/article/details/132364220
Recomendado
Clasificación