“Hapus node dalam daftar leetcode yang ditautkan” Kode Jawaban

Hapus node dalam daftar leetcode yang ditautkan

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

Hapus Elemen Daftar Tertaut 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

Jawaban yang mirip dengan “Hapus node dalam daftar leetcode yang ditautkan”

Pertanyaan yang mirip dengan “Hapus node dalam daftar leetcode yang ditautkan”

Lebih banyak jawaban terkait untuk “Hapus node dalam daftar leetcode yang ditautkan” di C++

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya