LintCode Linked List专辑

1. Reverse Linked List

/**
 * Definition of singly-linked-list:
 *
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: n
     * @return: The new head of reversed linked list.
     */
    ListNode * reverse(ListNode * head) {
        if (!head) return nullptr;
        ListNode *res = nullptr;
        while (head) {
            ListNode *next = head->next;
            head->next = res;
            res = head;
            head = next;
        }
        return res;
    }
};

2. Remove Duplicates from Sorted List

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: head is the head of the linked list
     * @return: head of linked list
     */
    ListNode * deleteDuplicates(ListNode * head) {
        if (!head) return nullptr;
        if(!head->next) return head;

        auto pre = head;
        auto cur = head->next;

        while (cur) {
            if (pre->val == cur->val) {
                pre->next = cur->next;
            } else {
                pre = cur;
            }
            cur = cur->next;
        }
        return head;
    }
};
  • concise way
/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: head is the head of the linked list
     * @return: head of linked list
     */
    ListNode * deleteDuplicates(ListNode * head) {
        if (!head) return nullptr;

        auto cur = head;
        while (cur->next) {
            if (cur->val == cur->next->val) {
                cur->next = cur->next->next;
            } else {
                cur = cur->next;
            }
        }
        return head;
    }
};

3. Remove Duplicates from Sorted List II

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: head is the head of the linked list
     * @return: head of the linked list
     */
    ListNode * deleteDuplicates(ListNode * head) {
        ListNode dummy{0, head};
        head = &dummy;

        while (head->next && head->next->next) {
            if (head->next->val == head->next->next->val) {
                while (head->next->next && head->next->val == head->next->next->val) {
                    // eg: 1 2 2 2
                    // in the end  of the loop `head->next` point to the last `2`
                    head->next = head->next->next;
                }
                // `head->next` point to `nullptr`
                head->next = head->next->next;
            } else {
                head = head->next;
            }
        }

        return dummy.next;
    }
};

4. Reverse Linked List II

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: ListNode head is the head of the linked list 
     * @param m: An integer
     * @param n: An integer
     * @return: The head of the reversed ListNode
     */
    ListNode * reverseBetween(ListNode * head, int m, int n) {
        // eg: 1->2->3->4->5, query(2, 4)
        if (m > n || !head) return nullptr;
        ListNode dummy{0, head};
        head = &dummy;

        // `mPrev` point to 1
        ListNode *mPrev = head;
        while (--m) mPrev = mPrev->next;
        // `nNext` point to 5
        ListNode *nNext = head;
        while (n--) nNext = nNext->next;
        nNext = nNext->next;

        // `tail` point to 2 which is the last element of the newly list
        ListNode *tail = mPrev->next;
        ListNode *cur = mPrev->next;
        ListNode *newHead = nullptr;
        while (cur != nNext) {
            ListNode *tmp = cur->next;
            cur->next = newHead;
            newHead = cur;
            cur = tmp;
        }
        // 2 -> 5
        tail->next = nNext;
        // 1 -> 4
        mPrev->next = newHead;

        return dummy.next;
    }
};

5. Majority Number III

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @param k: An integer
     * @return: The majority number
     */
    int majorityNumber(vector<int> &nums, int k) {
        size_t sz = nums.size() / k;
        map<int, int> freq;
        for (auto num: nums) {
            freq[num]++;
        }
        int f = 0;
        int res = 0;
        for (auto e: freq) {
            if (e.second > f) {
                f = e.second;
                res = e.first;
            }
        }
        return res;
    }
};

6. Partition List

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: The first node of linked list
     * @param x: An integer
     * @return: A ListNode
     */
    ListNode * partition(ListNode * head, int x) {
        ListNode leftDummy{0, nullptr};
        ListNode rightDummy{0, nullptr};
        auto leftHead = &leftDummy;
        auto rightHead = &rightDummy;
        while (head) {
            if (head->val < x) {
                leftHead->next = head;
                leftHead = leftHead->next;
            } else {
                rightHead->next = head;
                rightHead = rightHead->next;
            }
            head = head->next;
        }
        leftHead->next = rightDummy.next; // concate
        rightHead->next = nullptr;
        return leftDummy.next;
    }
};

7. Sort List

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list, using constant space complexity.
     */
    ListNode * sortList(ListNode * head) {
        if (!head || !head->next) return head;
        auto mid = _mid(head);
        auto left = sortList(mid->next);
        mid->next = nullptr;
        auto right = sortList(head);
        return merge(left, right);
    }

    ListNode *_mid(ListNode *head) {
        if (!head || !head->next) return head;
        auto slow = head;
        auto fast = head->next;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode *merge(ListNode *left, ListNode *right) {
        ListNode dummy(0, nullptr);
        auto head = &dummy;
        while (left && right) {
            auto l = left->val;
            auto r = right->val;
            if (l < r) {
                head->next = left;
                head = head->next;
                left = left->next;
            } else if (l > r) {
                head->next = right;
                head = head->next;
                right = right->next;
            } else {
                head->next = left;
                head = head->next;
                left = left->next;
                head->next = right;
                head = head->next;
                right = right->next;
            }
        }

        if (left) {
            head->next = left;
        }

        if (right) {
            head->next = right;
        }

        return dummy.next;
    }
};

8. Reorder List

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: The head of linked list.
     * @return: nothing
     */
    void reorderList(ListNode * head) {
        // 1. find mid of list
        if (!head) return;
        auto mid = _mid(head);
        // 2. reverse the right hand side
        auto right = reverse(mid->next);
        mid->next = nullptr;
        // 3. merge the list
        merge(head, right);
    }

    ListNode *merge(ListNode *left, ListNode *right) {
        ListNode dummy(0, nullptr);
        auto head = &dummy;
        while (left && right) {
            head->next = left;
            head = head->next;
            left = left->next;
            head->next = right;
            head = head->next;
            right = right->next;
        }
        if (left) {
            head->next = left;
        }
        if (right) {
            head->next = right;
        }
        return dummy.next;
    }

    ListNode *reverse(ListNode *head) {
        if (!head || !head->next) return head;
        ListNode *res = nullptr;
        while (head) {
            auto tmp = head->next;
            head->next = res;
            res = head;
            head = tmp;
        }
        return res;
    }

    ListNode *_mid(ListNode *head) {
        if (!head->next) return head;
        auto slow = head;
        auto fast = head->next;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
};

9. Linked List Cycle

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param head: The first node of linked list.
     * @return: True if it has a cycle, or false
     */
    bool hasCycle(ListNode * head) {
        if (!head || !head->next) return false;
        auto slow = head;
        auto fast = head->next;
        while (fast && fast->next) {
            if (slow == fast) return true;
            slow = slow->next;
            fast = fast->next->next;
        }
        return false;
    }
};

10. Linked List Cycle II

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param head: The first node of linked list.
     * @return: The node where the cycle begins. if there is no cycle, return null

     s -> .. -> b --> ...
           ^
           |______c|

    dis(slow) = sb + bc
    dis(fast) = sb + L + bc

    time cost is equal, so:
    dis(slow) = dis(fast) / 2
    =>
    2sb + 2bc = sb + L + bc
    sb + bc = L
    bc + cb = L

    => sb = bc
     */
    ListNode * detectCycle(ListNode * head) {
        if (!head) return head;
        auto slow = head;
        auto fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (fast == slow) break;
        }
        if (fast != slow) return nullptr;
        if (fast == head && fast->next != head) return nullptr;
        // cout << "cycle meet at: " << slow->val << endl;
        slow = head;
        while (slow != fast) {
            slow = slow->next;
            fast = fast->next;
        }
        return slow;
    }
};

11. Merge K sorted Lists

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param lists: a list of ListNode
     * @return: The head of one sorted list.
     */
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        ListNode dummy(0, nullptr);
        auto head = &dummy;
        auto cmp = [] (ListNode *l, ListNode *r) {
            return l->val > r->val;
        };
        priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq(cmp);

        // 1. all heads into the list
        for (auto list: lists) {
            if (list) pq.push(list);
        }
        while (!pq.empty()) {
            auto cur = pq.top();
            cout << cur->val << endl;
            pq.pop();
            head->next = cur;
            head = head->next;
            // 2. push the next element of current list into pq
            if (cur->next) pq.push(cur->next);
        }
        return dummy.next;
    }
};

12. Merge Two Sorted Lists

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param l1: ListNode l1 is the head of the linked list
     * @param l2: ListNode l2 is the head of the linked list
     * @return: ListNode head of linked list
     */
    ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
        ListNode dummy(0, nullptr);
        auto head = &dummy;
        while (l1 && l2) {
            auto lv = l1->val;
            auto rv = l2->val;
            if (lv < rv) {
                head->next = l1;
                head = head->next;
                l1 = l1->next;
            } else if (lv > rv) {
                head->next = l2;
                head = head->next;
                l2 = l2->next;
            } else {
                head->next = l1;
                head = head->next;
                l1 = l1->next;
                head->next = l2;
                head = head->next;
                l2 = l2->next;
            }
        }
        if (l1) {
            head->next = l1;
        }
        if (l2) {
            head->next = l2;
        }
        return dummy.next;
    }
};

13. Nth to Last Node in List

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param head: The first node of linked list.
     * @param n: An integer
     * @return: Nth to last node of a singly linked list. 
     */
    ListNode * nthToLast(ListNode * head, int n) {
        if (!head) return head;
        auto slow = head;
        auto fast = head;
        while (--n) fast = fast->next;
        while (fast->next) {
            fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
};

14. Add Two Numbers


/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the sum list of l1 and l2 
     */
    ListNode * addLists(ListNode * l1, ListNode * l2) {
        ListNode dummy(0, nullptr);
        auto head = &dummy;
        int carrage = 0;
        while (l1 && l2) {
            auto sum = l1->val + l2->val + carrage;
            carrage = sum >= 10;
            sum = sum % 10;
            head->next = new ListNode{sum, nullptr};
            head = head->next;
            l1 = l1->next;
            l2 = l2->next;
        }
        while (l1) {
            auto sum = l1->val + carrage;
            carrage = sum >= 10;
            sum = sum % 10;
            head->next = new ListNode{sum, nullptr};
            head = head->next;
            l1 = l1->next;
        }
        while (l2) {
            auto sum = l2->val + carrage;
            carrage = sum >= 10;
            sum = sum % 10;
            head->next = new ListNode{sum, nullptr};
            head = head->next;
            l2 = l2->next;
        }
        if (carrage) {
            head->next = new ListNode{1, nullptr};
            head = head->next;
        }
        return dummy.next;
    }
};

15. Count Linked List Nodes

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: the first node of linked list.
     * @return: An integer
     */
    int countNodes(ListNode * head) {
        int res = 0;
        while (head) {
            ++res;
            head = head->next;
        }
        return res;
    }
};

16. Remove Linked List Elements

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: a ListNode
     * @param val: An integer
     * @return: a ListNode
     */
    ListNode * removeElements(ListNode * head, int val) {
        ListNode dummy(0, head);
        auto pre = &dummy;
        while (head) {
            if (head->val == val) {
                pre->next = head->next;
            } else {
                pre = pre->next;
            }
            head = head->next;
        }
        return dummy.next;
    }
};

17.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值