LeetCode Solutions
Table of Content
Valid Parentheses
easy
Given a string s containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Approach
- create a stack
storeParentheses
to store parentheses for comparison - store first parentheses in stack and compare the other one with the one present in stack
- if they match then pop the character from stack else return false (meaning it doesn’t have valid parentheses)
Solution
bool isValid(string s)
{
stack<char> storeParentheses;
for (char ch : s)
{
if (ch == '(' || ch == '{' || ch == '[')
{
storeParentheses.push(ch);
}
else if (!storeParentheses.empty() &&
((ch == ')' && storeParentheses.top() == '(') ||
(ch == '}' && storeParentheses.top() == '{') ||
(ch == ']' && storeParentheses.top() == '[')))
{
storeParentheses.pop();
}
else
{
return false;
}
}
return storeParentheses.empty();
}
Merge Two Lists
easy
You are given the heads of two sorted linked lists list1
and list2
.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Approach
- if
list1
is empty returnlist2
- if
list2
is empty returnlist1
- Create a new list to add up both lists
- run a loop till both
list1
andlist2
becomes empty (nullptr
) - if
list1
value is smaller thanlist2
value add it tomergedList
- else add
list2
value tomergedList
- if
list1
is not empty (nullptr
)current->next
will belist1
else it’ll be eq tolist2
- Remove head from
mergedList
, deletemergedList
and return theresult
[!NOTE] if we don’t remove
head
ofmergedList
then the list starts from0
.
Solution
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2)
{
if (list1 == nullptr)
{
return list2;
}
if (list2 == nullptr)
{
return list1;
}
ListNode *mergedList = new ListNode(0);
ListNode *current = mergedList;
while (list1 != nullptr && list2 != nullptr)
{
if (list1->val < list2->val)
{
current->next = list1;
list1 = list1->next;
}
else
{
current->next = list2;
list2 = list2->next;
}
current = current->next;
}
if (list1 != nullptr)
{
current->next = list1;
}
else
{
current->next = list2;
}
ListNode *result = mergedList->next;
delete mergedList;
return result;
}