Skip to content

Commit ca13ec7

Browse files
authored
Merge Two Sorted Lists - Two Pointer (#159)
* Time needed to inform all employees Time needed to inform all employees - Depth First Search. * Update README.md * Added Merge Two Sorted Lists - Two Pointer * Updated Read Me
1 parent 41a9554 commit ca13ec7

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

C++/Merge-Two-Sorted-Lists.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Time Complexity : O(n)
2+
// Space Complexity : O(1)
3+
4+
5+
6+
Problem: Merge two sorted Lists
7+
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* struct ListNode {
12+
* int val;
13+
* ListNode *next;
14+
* ListNode() : val(0), next(nullptr) {}
15+
* ListNode(int x) : val(x), next(nullptr) {}
16+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
22+
if(l1 == NULL)
23+
return l2;
24+
if(l2 == NULL)
25+
return l1;
26+
27+
if(l1 -> val <= l2 -> val)
28+
{
29+
l1 -> next = mergeTwoLists(l1 -> next, l2);
30+
return l1;
31+
}
32+
33+
else
34+
{
35+
l2 -> next = mergeTwoLists(l1, l2 -> next);
36+
return l2;
37+
}
38+
}
39+
};
40+
41+

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
279279
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Java](./Java/median-of-two-sorted-arrays.java) | _O(log(min(m,n)))_ | _O(1)_ | Hard | | |
280280
| 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer |
281281
| 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | |
282+
| 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | |
282283

283284

284285
<br/>
@@ -335,7 +336,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
335336
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [python](./Python/maximum-depth-of-binary-tree.py) | _O(n)_ | _O(n)_ | Easy | DFS | |
336337
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
337338
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
338-
| 1376 | [ Time Needed to Inform All Employees] (https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++] | _O(n)_ | _O(n)_ | Medium | DFS | |
339+
| 1376 | [ Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | |
339340
|<br/>
340341
<div align="right">
341342
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -482,6 +483,11 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
482483
| [Gamez0](https://github.com/Gamez0) <br> <img src="https://avatars3.githubusercontent.com/u/34051876?s=400&v=4" width="100" height="100"> | South Korea | Python | [LeetCode](https://leetcode.com/Gamez0/) |
483484
| [JeongDaHyeon](https://github.com/JeongDaHyeon) <br> <img src="https://avatars0.githubusercontent.com/u/48541114?s=460&v=4" width="100" height="100"> | South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) |
484485
[Aysia](https://www.linkedin.com/in/aysiaelise/) <br> <img src="https://avatars.githubusercontent.com/u/70167431?s=460&u=1637be8636b6db6e35343ed9c1318c23e909b463&v=4" width="100" height="100"> | USA | JavaScript | [GitHub](https://github.com/aysiae)
486+
| [Poorvi Garg](https://github.com/POORVI111) <br> <img src="https://avatars.githubusercontent.com/u/68559217?s=400&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/POORVI111)
487+
488+
489+
490+
485491
<br/>
486492
<div align="right">
487493
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)