diff --git a/cpp/0021-merge-two-sorted-lists.cpp b/cpp/0021-merge-two-sorted-lists.cpp index febb22345..cae89422e 100644 --- a/cpp/0021-merge-two-sorted-lists.cpp +++ b/cpp/0021-merge-two-sorted-lists.cpp @@ -31,16 +31,8 @@ class Solution { return list1; } - ListNode* head = NULL; - if (list1->val <= list2->val) { - head = list1; - list1 = list1->next; - } else { - head = list2; - list2 = list2->next; - } - ListNode* curr = head; - + ListNode* dummy = new ListNode(); + ListNode *curr = dummy; while (list1 != NULL && list2 != NULL) { if (list1->val <= list2->val) { curr->next = list1; @@ -58,6 +50,6 @@ class Solution { curr->next = list1; } - return head; + return dummy->next; } }; diff --git a/python/0128-longest-consecutive-sequence.py b/python/0128-longest-consecutive-sequence.py index 4a2168ba4..11c63a8e8 100644 --- a/python/0128-longest-consecutive-sequence.py +++ b/python/0128-longest-consecutive-sequence.py @@ -3,7 +3,7 @@ def longestConsecutive(self, nums: List[int]) -> int: numSet = set(nums) longest = 0 - for n in nums: + for n in numSet: # check if its the start of a sequence if (n - 1) not in numSet: length = 1