Skip to content

Commit 07f514b

Browse files
committed
day 1
1 parent 0192ba6 commit 07f514b

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Palindrome Linked List
3+
======================
4+
5+
Given the head of a singly linked list, return true if it is a palindrome.
6+
7+
Example 1:
8+
Input: head = [1,2,2,1]
9+
Output: true
10+
11+
Example 2:
12+
Input: head = [1,2]
13+
Output: false
14+
15+
Constraints:
16+
The number of nodes in the list is in the range [1, 105].
17+
0 <= Node.val <= 9
18+
19+
Follow up: Could you do it in O(n) time and O(1) space?
20+
*/
21+
22+
/**
23+
* Definition for singly-linked list.
24+
* struct ListNode {
25+
* int val;
26+
* ListNode *next;
27+
* ListNode() : val(0), next(nullptr) {}
28+
* ListNode(int x) : val(x), next(nullptr) {}
29+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
30+
* };
31+
*/
32+
class Solution
33+
{
34+
public:
35+
bool isPalindrome(ListNode *head)
36+
{
37+
auto slow = head, fast = head;
38+
while (fast && fast->next)
39+
{
40+
slow = slow->next;
41+
fast = fast->next->next;
42+
}
43+
auto head2 = reverse(slow);
44+
auto head1 = head;
45+
while (head2 && head1)
46+
{
47+
if (head1->val != head2->val)
48+
return false;
49+
head1 = head1->next;
50+
head2 = head2->next;
51+
}
52+
return true;
53+
}
54+
55+
ListNode *reverse(ListNode *mid)
56+
{
57+
if (!mid || !mid->next)
58+
return mid;
59+
auto curr = mid, next = curr->next;
60+
curr->next = NULL;
61+
while (next)
62+
{
63+
auto nextNext = next->next;
64+
next->next = curr;
65+
curr = next;
66+
next = nextNext;
67+
}
68+
69+
return curr;
70+
}
71+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# April 2021 LeetCoding Challenge
2+
3+
| Day | Question Links | Solutions |
4+
| :-: | :------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------: |
5+
| 1. | [Palindrome Linked List](https://leetcode.com/explore/featured/card/april-leetcoding-challenge-2021/593/week-1-april-1st-april-7th/3693/) | [cpp](./01.%20Palindrome%20Linked%20List.cpp) |

Leetcode Daily Challenge/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
- [December](./December-2020)
88

99
### 2021
10+
1011
- [January](./January-2021)
1112
- [February](./February-2021)
12-
- [March](./March-2021)
13+
- [March](./March-2021)
14+
- [April](./April-2021)

README.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,26 @@ Urls for other Notes, and Reading Resources picked from different editorials.
165165
- [📘 CPP Syntax Cheet Sheet](./Resources/Articles/CPP-SYNTAX.md)
166166
</details>
167167

168+
<details>
169+
<summary>Last Minute Notes</summary>
170+
171+
- [📙 Last Minute Notes – Operating Systems](https://www.geeksforgeeks.org/last-minute-notes-operating-systems/)
172+
- [📙 Last Minute Notes – Computer Networks](https://www.geeksforgeeks.org/last-minute-notes-computer-network/)
173+
- [📙 Last Minute Notes – DBMS](https://www.geeksforgeeks.org/last-minute-notes-dbms/)
174+
- [📙 Last Minute Notes – Algorithms](https://www.geeksforgeeks.org/lmns-algorithms-gq/)
175+
- [📙 Last Minute Notes – Data Structure](https://www.geeksforgeeks.org/lmns-gq/lmns-data-structure-gq/)
176+
</details>
177+
168178
<details>
169179
<summary>Notes/Question/Explanations</summary>
170180

171-
- [📒 Dynamic Programming and Bit Masking](./Resources/Notes/Dynamic%20Programming%20and%20Bit%20Masking.md)
172-
- [📒 Disjoint Set Union (Union Find)](./Resources/Notes/Disjoint%20Set%20Union.md)
173-
- [📒 Ternary Search](./Resources/Notes/Ternary%20Search.md)
174-
- [📒 Binary Search for Beginners (leetcode)](https://leetcode.com/discuss/general-discussion/691825/Binary-Search-for-Beginners-Problems-or-Patterns-or-Sample-solutions)
175-
- [📕 Dynamic Programming (questions)](./Resources/Questions/Dynamic-Programming.md)
176-
- [📕 Union Find (questions)](./Resources/Questions/Union-Find.md)
177-
- [📕 Ternary Search (questions)](./Resources/Questions/Ternary-Search.md)
181+
- [📒 Dynamic Programming and Bit Masking](./Resources/Notes/Dynamic%20Programming%20and%20Bit%20Masking.md)
182+
- [📒 Disjoint Set Union (Union Find)](./Resources/Notes/Disjoint%20Set%20Union.md)
183+
- [📒 Ternary Search](./Resources/Notes/Ternary%20Search.md)
184+
- [📒 Binary Search for Beginners (leetcode)](https://leetcode.com/discuss/general-discussion/691825/Binary-Search-for-Beginners-Problems-or-Patterns-or-Sample-solutions)
185+
- [📕 Dynamic Programming (questions)](./Resources/Questions/Dynamic-Programming.md)
186+
- [📕 Union Find (questions)](./Resources/Questions/Union-Find.md)
187+
- [📕 Ternary Search (questions)](./Resources/Questions/Ternary-Search.md)
178188
</details>
179189

180190
<br />

0 commit comments

Comments
 (0)