Shortest non-recursive C++ code for 13ms


  • 0
    C
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* Mid = head, *Pre = new ListNode(0), *Newhead = Pre;
            int Number;
            
            Pre->next = head;
            while (Mid && Mid->next) {
                if (Mid->val == Mid->next->val) {
                    Number = Mid->val;
                    Mid = Mid->next->next; // this line saves 13 ms lol~!
                    while (Mid && Mid->val == Number) Mid = Mid->next;
                    Pre->next = Mid;
                }
                else {
                    Pre = Mid;
                    Mid = Mid->next;
                }
            }
            return Newhead->next;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.