“Löschen Sie den Knoten in einer verknüpften Liste Leetcode” Code-Antworten

Löschen Sie den Knoten in einer verknüpften Liste Leetcode

class Solution {
public:
    void deleteNode(ListNode* node) {
        //Make the given node to the next node and delete one of them since they now same
        //Say linked list is 4 -> 5 -> 1 -> 9, node = 5
        
        node -> val = node -> next -> val;
        
        //Now it'll be 4 -> 1 -> 1 -> 9, now our node is the first 1
        
        node -> next = node -> next -> next;
        
        //Now it'll be 4 -> 1 -> 9 (the second 1's next link is cut off)
        //That's the whole answer we don't have to return anything
    }
};
Mysterious Moth

Entfernen Sie verknüpfte Listeelemente Leetcode

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        /*while (head == NULL){
            return head;
        }
        No need for this funtion since we gotta return head in the end of the funtion anyways so this case wud be included
        */
        
        while (head != NULL && head -> val == val){
            head = head -> next;
        }
        ListNode* curr = head;
        while (curr != NULL && curr -> next != NULL){
            if (curr -> next -> val == val){
                curr -> next = curr -> next -> next;
            }
            else {
                curr = curr -> next;
            }
        }
    return head;
    }
};
Mysterious Moth

Ähnliche Antworten wie “Löschen Sie den Knoten in einer verknüpften Liste Leetcode”

Fragen ähnlich wie “Löschen Sie den Knoten in einer verknüpften Liste Leetcode”

Weitere verwandte Antworten zu “Löschen Sie den Knoten in einer verknüpften Liste Leetcode” auf C++

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen