Skip to content

Commit ed6c2b9

Browse files
committed
Solve problem: Longest Palindrome & Linked List Cycle II
1 parent fd0b16b commit ed6c2b9

File tree

11 files changed

+181
-44
lines changed

11 files changed

+181
-44
lines changed

README.MD

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ Solving problems with LeetCode
44

55
## Problems
66

7-
| Title | Solution | Difficulty |
8-
|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|------------|
9-
| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10-
| [Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11-
| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12-
| [Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13-
| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14-
| [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15-
| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16-
| [Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17-
| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18-
| [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19-
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20-
| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21-
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22-
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
23-
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
24-
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
25-
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
26-
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
27-
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
28-
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |
7+
| Title | Solution | Difficulty |
8+
|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|------------|
9+
| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10+
| [Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11+
| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12+
| [Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13+
| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14+
| [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15+
| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16+
| [Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17+
| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18+
| [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19+
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20+
| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21+
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22+
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
23+
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
24+
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
25+
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
26+
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
27+
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
28+
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |
29+
| [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/longestPalindrome/longestPalindrome.js) | Easy |
30+
| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/description/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/detectCycle/detectCycle.js) | Medium |

helpers/createLinkedListFromArray.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
const ListNode = require('./listNode');
2+
3+
/**
4+
* Creates a linked list from an array of values.
5+
*
6+
* @param {Array<number>} arr - The array of values to create the linked list from.
7+
* @returns {ListNode | null} The head node of the linked list, or null if the input array is empty.
8+
*/
19
function createLinkedListFromArray(arr) {
210
if (!arr || arr.length === 0) {
311
return null;
412
}
513

6-
let head = {
7-
val: arr[0],
8-
next: null,
9-
};
14+
let head = new ListNode(arr[0]);
1015
let current = head;
1116

1217
for (let i = 1; i < arr.length; i++) {
13-
let newNode = {
14-
val: arr[i],
15-
next: null,
16-
};
18+
let newNode = new ListNode(arr[i]);
1719
current.next = newNode;
1820
current = newNode;
1921
}

helpers/listNode.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
2-
* Definition for singly-linked list.
3-
* function ListNode(val, next) {
4-
* this.val = (val===undefined ? 0 : val)
5-
* this.next = (next===undefined ? null : next)
6-
* }
2+
* A node in a singly-linked list.
3+
*
4+
* @constructor
5+
* @param {number} val - The numeric value of the node.
6+
* @property {number} val - The numeric value of the node.
7+
* @property {ListNode | null} next - The next node in the linked list, or null if this is the last node.
78
*/
89
function ListNode(val) {
910
this.val = val;

solutions/detectCycle/README.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/description/)
2+
3+
Given the `head` of a linked list, return the node where the cycle begins. If there is no cycle, return `null`.
4+
5+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (0-indexed). It is `-1` if there is no cycle. Note that `pos` is not passed as a parameter.
6+
7+
Do not modify the linked list.

solutions/detectCycle/detectCycle.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {ListNode} head
3+
* @return {ListNode}
4+
*/
5+
function detectCycle(head) {
6+
const set = new Set();
7+
8+
while (head) {
9+
set.add(head);
10+
head = head.next;
11+
12+
if (set.has(head)) {
13+
return head;
14+
}
15+
}
16+
17+
return null;
18+
}
19+
20+
module.exports = detectCycle;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const assert = require('node:assert');
2+
const detectCycle = require('./detectCycle');
3+
const createLinkedListFromArray = require('../../helpers/createLinkedListFromArray');
4+
5+
describe('detectCycle', function () {
6+
it('should return null for null input', function () {
7+
const head = null;
8+
const result = detectCycle(head);
9+
assert.strictEqual(result, null);
10+
});
11+
12+
it('should return null for non-cyclic list', function () {
13+
const arr = [1, 2, 3, 4, 5];
14+
const head = createLinkedListFromArray(arr);
15+
const result = detectCycle(head);
16+
assert.strictEqual(result, null);
17+
});
18+
19+
it('should return node at start of cycle', function () {
20+
const arr = [1, 2, 3, 4, 5];
21+
const head = createLinkedListFromArray(arr);
22+
const lastNode = head.next.next.next.next;
23+
lastNode.next = head.next;
24+
const result = detectCycle(head);
25+
assert.strictEqual(result, head.next);
26+
});
27+
28+
it('should return node in middle of cycle', function () {
29+
const arr = [1, 2, 3, 4, 5];
30+
const head = createLinkedListFromArray(arr);
31+
const lastNode = head.next.next.next.next;
32+
lastNode.next = head.next.next;
33+
const result = detectCycle(head);
34+
assert.strictEqual(result, head.next.next);
35+
});
36+
37+
it('should return node at end of cycle', function () {
38+
const arr = [1, 2, 3, 4, 5];
39+
const head = createLinkedListFromArray(arr);
40+
const lastNode = head.next.next.next.next;
41+
lastNode.next = lastNode;
42+
const result = detectCycle(head);
43+
assert.strictEqual(result, lastNode);
44+
});
45+
});

solutions/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val: number);
5+
}

solutions/longestPalindrome/README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description/)
2+
3+
Given a string `s` which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
4+
5+
Letters are case sensitive, for example, `"Aa"` is not considered a palindrome here.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
function longestPalindrome(s) {
6+
const counter = {};
7+
let result = 0;
8+
let hasOdd = false;
9+
10+
s.split('').forEach(char => {
11+
if (!counter[char]) {
12+
counter[char] = 0;
13+
}
14+
counter[char]++;
15+
});
16+
17+
for (const key in counter) {
18+
if (counter[key] % 2) {
19+
result += counter[key] - 1;
20+
hasOdd = true;
21+
} else {
22+
result += counter[key];
23+
}
24+
}
25+
26+
result += hasOdd ? 1 : 0;
27+
28+
return result;
29+
}
30+
31+
module.exports = longestPalindrome;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const longestPalindrome = require('./longestPalindrome');
2+
3+
describe('Longest Palindrome', () => {
4+
it('longestPalindrome returns correct result for empty string', () => {
5+
expect(longestPalindrome('')).toBe(0);
6+
});
7+
8+
it('longestPalindrome returns correct result for string with no palindrome', () => {
9+
expect(longestPalindrome('abc')).toBe(1);
10+
});
11+
12+
it('longestPalindrome returns correct result for string with one palindrome', () => {
13+
expect(longestPalindrome('racecar')).toBe(7);
14+
});
15+
16+
it('longestPalindrome returns correct result for string with multiple palindromes', () => {
17+
expect(longestPalindrome('abccccdd')).toBe(7);
18+
});
19+
20+
it('longestPalindrome returns correct result for string with odd palindrome', () => {
21+
expect(longestPalindrome('abbcccddddeeeee')).toBe(13);
22+
});
23+
24+
it('longestPalindrome returns correct result for string with even palindrome', () => {
25+
expect(longestPalindrome('abcdedcba')).toBe(9);
26+
});
27+
});

solutions/middleNode/index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* function ListNode(val, next) {
4-
* this.val = (val===undefined ? 0 : val)
5-
* this.next = (next===undefined ? null : next)
6-
* }
7-
*/
8-
91
/**
102
* @param {ListNode} head
113
* @return {ListNode}
@@ -26,4 +18,4 @@ function middleNode(head) {
2618
return values[Math.floor(values.length / 2)];
2719
}
2820

29-
module.exports = middleNode;
21+
module.exports = middleNode;

0 commit comments

Comments
 (0)