鏈表 | 刪除鏈表的節點

問題:刪除指定值的節點

題目鏈接
在這裏插入圖片描述

解題思路

新增一個頭節點可以方便處理。

C++代碼

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *list = new ListNode(0);//增加一個頭節點
        ListNode *tail = list, *cur = head;
        while(cur){
            if(cur->val != val){//不是待刪除的節點
                tail->next = cur;
                tail = cur;
                cur = cur->next;
            }
            else{//是待刪除的節點
                ListNode *tmp = cur->next;
                delete cur;//刪除此節點
                cur = tmp;
            }
        }
        tail->next = NULL;
        return list->next;
    }
};

問題:刪除指定節點

問題鏈接
在這裏插入圖片描述
在這裏插入圖片描述

解題思路

由於只給定了待刪除的節點,我們不知道此節點的前驅節點,因此無法刪除。但是,由於待刪除節點不是尾節點,因此可以讓此節點的值設爲後驅節點的值,然後將後驅節點刪除即可。

C++代碼

/**
 * 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) {
        node->val = node->next->val;
        ListNode *tmp = node->next;
        node->next = node->next->next;
        delete tmp;
    }
};

問題:刪除排序鏈表中的重複元素

問題鏈接
在這裏插入圖片描述

解題思路

由於是有序鏈表,因此節點值相等的節點相鄰,因此只要比較當前節點的值是否等於上一個保留的節點的值即可。

C++代碼

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode *tail = head, *cur = head->next;
        while(cur){
            if(cur->val != tail->val){//不重複,加入此節點
                tail->next = cur;
                tail = cur;
                cur = cur->next;
            }
            else{//重複
                ListNode *tmp = cur->next;//記錄下一個節點
                delete cur;
                cur = tmp;
            }
        }
        tail->next = NULL;//tail是最後一個節點
        return head;
    }
};

問題:刪除排序鏈表中的重複元素 II

問題鏈接
在這裏插入圖片描述

解題思路

由於重複的元素必須全部刪除,因此可以使用一個map<int,int>記錄每個元素出現的次數,然後刪除重複的元素即可。

C++代碼

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        map<int, int>dict;//記錄各個數字出現的次數
        ListNode *cur = head;
        while(cur){
            dict[cur->val]++;
            cur = cur->next;
        }
        //新建一個頭節點,方便處理
        ListNode *list = new ListNode(0), *tail = list;
        cur = head;
        while(cur){
            if(dict[cur->val] > 1){ //是重複的,刪除
                ListNode *tmp = cur->next;
                delete cur;
                cur = tmp;
            }
            else{ //不是重複的
                tail->next = cur;
                tail = cur;
                cur = cur->next;
            }
        }
        tail->next = NULL;//重要,tail是最後一個節點 !!!!
        return list->next;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章