面試_鏈表類

劍指 Offer 24. 反轉鏈表

定義一個函數,輸入一個鏈表的頭節點,反轉該鏈表並輸出反轉後鏈表的頭節點。

示例:

輸入: 1->2->3->4->5->NULL
輸出: 5->4->3->2->1->NULL

限制:

0 <= 節點個數 <= 5000
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        //法一:
        // if (head == nullptr) return nullptr;
        // ListNode *pre = nullptr, *cur = head;
        // while (cur)
        // {
        //     ListNode *t = cur->next;
        //     cur->next = pre; pre = cur;
        //     cur = t;
        // }
        // return pre;

        //法二
        if (head == nullptr) return nullptr;
        ListNode *pre = new ListNode(head->val); head = head->next;
        while (head)
        {
            ListNode *t = new ListNode(head->val);
            t->next = pre; pre = t;
            head = head->next;
        }
        return pre;

        // if (!head || !head->next) return head;
        // ListNode *newHead = reverseList(head->next);
        // head->next->next = head;
        // head->next = NULL;
        // return newHead;
    }
};

劍指 Offer 22. 鏈表中倒數第k個節點

輸入一個鏈表,輸出該鏈表中倒數第k個節點。爲了符合大多數人的習慣,本題從1開始計數,即鏈表的尾節點是倒數第1個節點。

例如,一個鏈表有 6 個節點,從頭節點開始,它們的值依次是 1、2、3、4、5、6。這個鏈表的倒數第 3 個節點是值爲 4 的節點。

示例:

給定一個鏈表: 1->2->3->4->5, 和 k = 2.

返回鏈表 4->5.
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        ListNode *fast = head, *slow = head;
        while (fast && k--)
        {
            fast = fast->next;
        }
        while (fast && slow)
        {
            fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
};

O(N)

劍指 Offer 18. 刪除鏈表的節點

給定單向鏈表的頭指針和一個要刪除的節點的值,定義一個函數刪除該節點。

返回刪除後的鏈表的頭節點。注意:此題對比原題有改動

示例 1:

輸入: head = [4,5,1,9], val = 5
輸出: [4,1,9]
解釋: 給定你鏈表中值爲 5 的第二個節點,那麼在調用了你的函數之後,該鏈表應變爲 4 -> 1 -> 9.
示例 2:

輸入: head = [4,5,1,9], val = 1
輸出: [4,5,9]
解釋: 給定你鏈表中值爲 1 的第三個節點,那麼在調用了你的函數之後,該鏈表應變爲 4 -> 5 -> 9.

說明:

題目保證鏈表中節點的值互不相同
若使用 C 或 C++ 語言,你不需要 free 或 delete 被刪除的節點
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
        if (head == nullptr) return nullptr;
        if (head->val == val) return head->next;
        ListNode *cur = head->next, *pre = head;
        while (cur && cur->val != val)
        {   
            pre = cur;
            cur = cur->next;
        }
        if (cur) {
            pre->next = cur->next;
        }
        return head;
    }
};

劍指 Offer 35. 複雜鏈表的複製

請實現 copyRandomList 函數,複製一個複雜鏈表。在複雜鏈表中,每個節點除了有一個 next 指針指向下一個節點,還有一個 random 指針指向鏈表中的任意節點或者 null。

示例 1:

輸入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
輸出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:
輸入:head = [[1,1],[2,1]]
輸出:[[1,1],[2,1]]

示例 3:
輸入:head = [[3,null],[3,0],[3,null]]
輸出:[[3,null],[3,0],[3,null]]

示例 4:
輸入:head = []
輸出:[]

解釋:給定的鏈表爲空(空指針),因此返回 null。

提示

-10000 <= Node.val <= 10000
Node.random 爲空(null)或指向鏈表中的節點。
節點數目不超過 1000 。
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    // Node* copyRandomList(Node* head) {
    //     if (head == nullptr) return nullptr;
    //     Node *cur = head;
    //     unordered_map<Node*, Node*> dic;
    //     while (cur)
    //     {
    //         dic[cur] = new Node(cur->val);
    //         cur = cur->next;
    //     }
    //     cur = head;
    //     while(cur)
    //     {
    //         dic[cur]->next = dic[cur->next];
    //         dic[cur]->random = dic[cur->random];
    //         cur = cur->next;
    //     }
    //     return dic[head];
    // }
    Node* copyRandomList(Node* head)
    {
        if (head == nullptr) return nullptr;
        Node *cur = head;
        // 1. 複製各節點,並構建拼接鏈表
        while (cur != nullptr)
        {
            Node *tmp = new Node(cur->val);
            //插入新節點 tmp (tmp == cur)
            tmp->next = cur->next; 
            cur->next = tmp;   
            cur = tmp->next;
        }
        //2. 構建各個新節點的random指向
        cur = head;
        while (cur != nullptr)
        {
            if (cur->random != nullptr) {
                //cur->next是cur的新節點
                //cur->random->next是cur->random的新節點
                cur->next->random = cur->random->next;
            }
            //cur真正next是 cur->next->next
            cur = cur->next->next;
        }
        //3. 拆分兩個鏈表
        //新節點頭節點
        cur = head->next;
        Node *pre = head, *res = head->next;
        while (cur->next != nullptr)
        {
            //建立原始結點鏈表
            pre->next = pre->next->next;
            //新節點鏈表
            cur->next = cur->next->next;
            pre = pre->next;
            cur = cur->next;
        }
        //如果這條不寫,相當於修改了原始鏈表最後一個元素的指針
        pre->next = nullptr;
        //返回新鏈表頭節點
        return res;
    }
};

劍指 Offer 36. 二叉搜索樹與雙向鏈表

輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的循環雙向鏈表。要求不能創建任何新的節點,只能調整樹中節點指針的指向。爲了讓您更好地理解問題,以下面的二叉搜索樹爲例:

我們希望將這個二叉搜索樹轉化爲雙向循環鏈表。鏈表中的每個節點都有一個前驅和後繼指針。對於雙向循環鏈表,第一個節點的前驅是最後一個節點,最後一個節點的後繼是第一個節點。

下圖展示了上面的二叉搜索樹轉化成的鏈表。“head” 表示指向鏈表中有最小元素的節點。

特別地,我們希望可以就地完成轉換操作。當轉化完成以後,樹中節點的左指針需要指向前驅,樹中節點的右指針需要指向後繼。還需要返回鏈表中的第一個節點的指針。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node() {}

    Node(int _val) {
        val = _val;
        left = NULL;
        right = NULL;
    }

    Node(int _val, Node* _left, Node* _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
    Node *pre, *head;
    void dfs(Node *cur)
    {
        if (cur == nullptr) return;
        dfs(cur->left);
        if (pre == nullptr) {
            head = cur;       //記錄頭指針
        } else {
            pre->right = cur;
        }
        cur->left = pre;
        pre = cur;
        dfs(cur->right);
    }
public:
    Node* treeToDoublyList(Node* root) {
        if (!root) return nullptr;
        dfs(root);
        head->left = pre;
        pre->right = head;
        return head;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章