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

add valuable comments and variable names #367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 33 additions & 15 deletions merge sorted list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ struct ListNode {
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0); // Create a dummy node
ListNode* current = &dummy; // Pointer to build the new list
// If one of the lists is empty, return the other
if (!list1) return list2;
if (!list2) return list1;

while (list1 != nullptr && list2 != nullptr) {
ListNode dummy(0); // Create a dummy node as a starting point
ListNode* current = &dummy; // Pointer to build the merged list

while (list1 && list2) {
if (list1->val <= list2->val) {
current->next = list1; // Attach list1 node
current->next = list1;
list1 = list1->next;
} else {
current->next = list2; // Attach list2 node
current->next = list2;
list2 = list2->next;
}
current = current->next; // Move current to the next position
current = current->next; // Move to the next node in the merged list
}

// Attach remaining nodes
current->next = (list1 != nullptr) ? list1 : list2;
// Attach remaining nodes from either list1 or list2
current->next = list1 ? list1 : list2;

return dummy.next; // Return the head of the merged list
}
Expand All @@ -34,24 +38,26 @@ class Solution {
// Function to create a linked list from user input
ListNode* createList() {
int n;
cout << "Enter number of nodes in the list: ";
cout << "Enter the number of nodes in the list: ";
cin >> n;

if (n <= 0) return nullptr; // Return null for empty lists
if (n <= 0) return nullptr; // Return null for an empty list

ListNode* head = nullptr;
ListNode* tail = nullptr;

cout << "Enter " << n << " values (in sorted order): ";
for (int i = 0; i < n; ++i) {
int value;
cin >> value;
ListNode* newNode = new ListNode(value);

if (!head) {
head = newNode;
head = newNode;
tail = head;
} else {
tail->next = newNode;
tail = tail->next;
tail->next = newNode;
tail = tail->next;
}
}

Expand All @@ -61,11 +67,20 @@ ListNode* createList() {
// Function to print the linked list
void printList(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
while (current) {
cout << current->val << " ";
current = current->next;
}
cout << std::endl;
cout << endl;
}

// Function to free memory of the linked list
void deleteList(ListNode* head) {
while (head) {
ListNode* temp = head;
head = head->next;
delete temp;
}
}

int main() {
Expand All @@ -82,5 +97,8 @@ int main() {
cout << "Merged sorted linked list:\n";
printList(mergedList);

// Free memory of all lists to prevent memory leaks
deleteList(mergedList);

return 0;
}