category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

21. Merge Two Sorted Lists

Implementation, Constant space ```cpp /** * 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* one, ListNode* two) { ListNode* dummy = new ListNode(); ListNode* head = dummy; while (one != nullptr && two != nullptr) { if (one->val < two->val) { dummy->next = one; one = one->next; } else { dummy->next = two; two = two->next; } dummy = dummy->next; } if (one != nullptr) dummy->next = one; if (two != nullptr) dummy->next = two; return head->next; } }; ```