Skip to content

Commit c8515ee

Browse files
author
shumbul
committed
Day-7
1 parent 25061e9 commit c8515ee

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

Day-07/39. Swap Nodes in Pairs.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Link - https://leetcode.com/problems/swap-nodes-in-pairs/
2+
// Author - Shumbul Arifa
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* struct ListNode {
7+
* int val;
8+
* ListNode *next;
9+
* ListNode() : val(0), next(nullptr) {}
10+
* ListNode(int x) : val(x), next(nullptr) {}
11+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
ListNode* swapPairs(ListNode* head) {
17+
auto tmp = head;
18+
while (tmp && tmp->next) {
19+
int t = tmp->val;
20+
tmp->val = tmp->next->val;
21+
tmp->next->val = t;
22+
tmp = tmp->next->next;
23+
}
24+
return head;
25+
}
26+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Link - https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
2+
// Author - Shumbul Arifa
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13+
* };
14+
*/
15+
class Solution {
16+
TreeNode* prev = NULL;
17+
public:
18+
void flatten(TreeNode* root) {
19+
if (!root)
20+
return;
21+
if (root->right)
22+
flatten(root->right);
23+
if (root->left)
24+
flatten(root->left);
25+
root->right = prev;
26+
root->left = NULL;
27+
prev = root;
28+
}
29+
};

Day-07/41. Partition List.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Link - https://leetcode.com/problems/partition-list/
2+
// Author - Shumbul Arifa
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* struct ListNode {
7+
* int val;
8+
* ListNode *next;
9+
* ListNode() : val(0), next(nullptr) {}
10+
* ListNode(int x) : val(x), next(nullptr) {}
11+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
ListNode* partition(ListNode* head, int x) {
17+
if (!head)
18+
return head;
19+
ListNode* l1 = new ListNode(0), *l2 = new ListNode(0);
20+
auto t1 = l1;
21+
auto t2 = l2;
22+
if (!l1 || !l2)
23+
return head;
24+
while (head) {
25+
// cout<<head->val<<"\n";
26+
if (head->val < x) {
27+
t1->next = head;
28+
t1 = t1->next;
29+
}
30+
else {
31+
t2->next = head;
32+
t2 = t2->next;
33+
}
34+
head = head->next;
35+
}
36+
t1->next = NULL;
37+
t2->next = NULL;
38+
t1->next = l2->next;
39+
return l1->next;
40+
}
41+
};

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,12 @@
7474
35. [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)
7575
36. [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)
7676
37. [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)
77-
38. [Path Sum](https://leetcode.com/problems/path-sum/)
77+
38. [Path Sum](https://leetcode.com/problems/path-sum/)
78+
79+
## Day-07
80+
09/09/21
81+
3 problems
82+
83+
39. [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)
84+
40. [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)
85+
41. [Partition List](https://leetcode.com/problems/partition-list/)

0 commit comments

Comments
 (0)