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

fix: 🎉 Merge Two Sorted Arrays : rndmcodeguy-addition #226

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* @lc app=leetcode id=1920 lang=cpp
*
* [1920] Build Array from Permutation
*/

class Solution
{
public:
vector<int> buildArray(vector<int> &nums)
{
vector<int> rez;
for (int i = 0; i < nums.size(); i++)
rez.push_back(nums[nums[i]]);
return rez;
}
};
73 changes: 73 additions & 0 deletions Leetcode-Easy/21-MergeTwoSortedList/21.Merge Two Sorted Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 list1;
}
else if (list1 == NULL && list2 != NULL)
{
return list2;
}
else if (list1 != NULL && list2 == NULL)
{
return list1;
}

ListNode *list3, *last;

if (list1->val < list2->val)
{
list3 = last = list1;
list1 = list1->next;
last->next = NULL;
}
else
{
list3 = last = list2;
list2 = list2->next;
last->next = NULL;
}

while (list1 && list2)
{
if (list1->val < list2->val)
{
last->next = list1;
list1 = list1->next;
last = last->next;
last->next = NULL;
}
else
{
last->next = list2;
list2 = list2->next;
last = last->next;
last->next = NULL;
}
}

if (list1 != NULL && list2 == NULL)
{
last->next = list1;
}
else
{
last->next = list2;
}

return list3;
}
};