neetcode-gh / leetcode

Leetcode solutions
MIT License
5.36k stars 2.24k forks source link

code reuse #2507

Open eliork opened 1 year ago

eliork commented 1 year ago

https://github.com/neetcode-gh/leetcode/blob/85d1f52d350efc88b3d8f53c973bfe47a97f4dc5/cpp/0021-merge-two-sorted-lists.cpp#L35-L42C1

there's code reuse here.

I'd suggest creating a new node, which will be the dummy, and returning head->next, like this:

ListNode* dummy = new ListNode();
ListNode* curr = dummy;

then return dummy->next

Bhavyaatrivedi commented 1 year ago

HI, I would like to work on this issue.

eliork commented 1 year ago

Here is what I think could be a good solution, feel free to comment

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if (list1 == NULL && list2 == NULL) {
            return NULL;
        } 
        if (list1 == NULL) {
            return list2;
        }
        if (list2 == NULL) {
            return list1;
        }
        ListNode* dummy = new ListNode();
        ListNode* curr = dummy;
        while (list1!=NULL && list2!=NULL) {
            if (list1->val <= list2->val) {
                curr->next = list1;
                list1 = list1->next;
            } else {
                curr->next = list2;
                list2 = list2->next;
            }
            curr = curr->next;
        }
        if (list1 == NULL) {
            curr->next = list2;
        }   
        if (list2 == NULL) {
            curr->next = list1;
        }        
        return dummy->next;
    }
};
Bhavyaatrivedi commented 1 year ago

PR created #2521

Here is what I think could be a good solution, feel free to comment

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if (list1 == NULL && list2 == NULL) {
            return NULL;
        } 
        if (list1 == NULL) {
            return list2;
        }
        if (list2 == NULL) {
            return list1;
        }
        ListNode* dummy = new ListNode();
        ListNode* curr = dummy;
        while (list1!=NULL && list2!=NULL) {
            if (list1->val <= list2->val) {
                curr->next = list1;
                list1 = list1->next;
            } else {
                curr->next = list2;
                list2 = list2->next;
            }
            curr = curr->next;
        }
        if (list1 == NULL) {
            curr->next = list2;
        }   
        if (list2 == NULL) {
            curr->next = list1;
        }        
        return dummy->next;
    }
};

PR created #2521

Bhavyaatrivedi commented 1 year ago

Here is what I think could be a good solution, feel free to comment

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if (list1 == NULL && list2 == NULL) {
            return NULL;
        } 
        if (list1 == NULL) {
            return list2;
        }
        if (list2 == NULL) {
            return list1;
        }
        ListNode* dummy = new ListNode();
        ListNode* curr = dummy;
        while (list1!=NULL && list2!=NULL) {
            if (list1->val <= list2->val) {
                curr->next = list1;
                list1 = list1->next;
            } else {
                curr->next = list2;
                list2 = list2->next;
            }
            curr = curr->next;
        }
        if (list1 == NULL) {
            curr->next = list2;
        }   
        if (list2 == NULL) {
            curr->next = list1;
        }        
        return dummy->next;
    }
};

Added the edge cases and fixed the code. PR created #2523