Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hash Map Program Addition ] --- Added the hashmap program. #2741

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file added Hashmap.cpp
Empty file.
40 changes: 40 additions & 0 deletions Merge_Two_sorted_lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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) {
// Create a dummy node to serve as the start of the merged list
ListNode* dummy = new ListNode(-1);
ListNode* current = dummy;

// Traverse both lists
while (list1 != nullptr && list2 != nullptr) {
// Compare the values in both lists and append the smaller one
if (list1->val <= list2->val) {
current->next = list1;
list1 = list1->next; // Move list1 pointer forward
} else {
current->next = list2;
list2 = list2->next; // Move list2 pointer forward
}
current = current->next; // Move the current pointer forward
}

// If one of the lists still has nodes left, append them
if (list1 != nullptr) {
current->next = list1;
} else {
current->next = list2;
}

// Return the next node after the dummy node, which is the head of the merged list
return dummy->next;
}
};