Skip to content

Commit b7d690d

Browse files
authored
feat: add solutions to lc/lcci problems (#2528)
* lcci No.02.06.Palindrome Linked List * lc No.3103.Find Trending Hashtags II
1 parent 3fec0c4 commit b7d690d

File tree

41 files changed

+820
-493
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+820
-493
lines changed

lcci/02.06.Palindrome Linked List/README.md

+159-146
Large diffs are not rendered by default.

lcci/02.06.Palindrome Linked List/README_EN.md

+159-146
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
bool isPalindrome(ListNode* head) {
12+
if (!head) {
13+
return true;
14+
}
15+
ListNode* slow = head;
16+
ListNode* fast = head->next;
17+
while (fast && fast->next) {
18+
slow = slow->next;
19+
fast = fast->next->next;
20+
}
21+
ListNode* p = slow->next;
22+
slow->next = nullptr;
23+
ListNode* dummy = new ListNode(0);
24+
while (p) {
25+
ListNode* next = p->next;
26+
p->next = dummy->next;
27+
dummy->next = p;
28+
p = next;
29+
}
30+
p = dummy->next;
31+
while (p) {
32+
if (head->val != p->val) {
33+
return false;
34+
}
35+
head = head->next;
36+
p = p->next;
37+
}
38+
return true;
39+
}
40+
};

lcci/02.06.Palindrome Linked List/Solution.cs

+15-22
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,37 @@
33
* public class ListNode {
44
* public int val;
55
* public ListNode next;
6-
* public ListNode(int val=0, ListNode next=null) {
7-
* this.val = val;
8-
* this.next = next;
9-
* }
6+
* public ListNode(int x) { val = x; }
107
* }
118
*/
129
public class Solution {
1310
public bool IsPalindrome(ListNode head) {
14-
if (head == null || head.next == null)
15-
{
11+
if (head == null) {
1612
return true;
1713
}
1814
ListNode slow = head;
1915
ListNode fast = head.next;
20-
while (fast != null && fast.next != null)
21-
{
16+
while (fast != null && fast.next != null) {
2217
slow = slow.next;
2318
fast = fast.next.next;
2419
}
25-
ListNode cur = slow.next;
20+
ListNode p = slow.next;
2621
slow.next = null;
27-
ListNode pre = null;
28-
while (cur != null)
29-
{
30-
ListNode t = cur.next;
31-
cur.next = pre;
32-
pre = cur;
33-
cur = t;
22+
ListNode dummy = new ListNode(0);
23+
while (p != null) {
24+
ListNode next = p.next;
25+
p.next = dummy.next;
26+
dummy.next = p;
27+
p = next;
3428
}
35-
while (pre != null)
36-
{
37-
if (pre.val != head.val)
38-
{
29+
p = dummy.next;
30+
while (p != null) {
31+
if (head.val != p.val) {
3932
return false;
4033
}
41-
pre = pre.next;
4234
head = head.next;
35+
p = p.next;
4336
}
4437
return true;
4538
}
46-
}
39+
}
+25-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func isPalindrome(head *ListNode) bool {
29
if head == nil {
310
return true
411
}
5-
m := mid(head)
6-
temp := reverse(m.Next)
7-
m.Next = nil
8-
p, q := head, temp
9-
res := true
10-
for p != nil && q != nil {
11-
if p.Val != q.Val {
12-
res = false
13-
break
14-
}
15-
p = p.Next
16-
q = q.Next
17-
}
18-
m.Next = reverse(temp)
19-
return res
20-
}
21-
22-
func mid(head *ListNode) *ListNode {
2312
slow, fast := head, head.Next
2413
for fast != nil && fast.Next != nil {
25-
slow = slow.Next
26-
fast = fast.Next.Next
14+
slow, fast = slow.Next, fast.Next.Next
2715
}
28-
return slow
29-
}
30-
31-
func reverse(head *ListNode) *ListNode {
32-
var prev *ListNode = nil
33-
for head != nil {
34-
temp := head.Next
35-
head.Next = prev
36-
prev = head
37-
head = temp
16+
p := slow.Next
17+
slow.Next = nil
18+
dummy := &ListNode{}
19+
for p != nil {
20+
next := p.Next
21+
p.Next = dummy.Next
22+
dummy.Next = p
23+
p = next
24+
}
25+
p = dummy.Next
26+
for p != nil {
27+
if head.Val != p.Val {
28+
return false
29+
}
30+
head = head.Next
31+
p = p.Next
3832
}
39-
return prev
33+
return true
4034
}

lcci/02.06.Palindrome Linked List/Solution.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode() {}
7-
* ListNode(int val) { this.val = val; }
8-
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
6+
* ListNode(int x) { val = x; }
97
* }
108
*/
119
class Solution {
1210
public boolean isPalindrome(ListNode head) {
13-
if (head == null || head.next == null) {
11+
if (head == null) {
1412
return true;
1513
}
1614
ListNode slow = head;
@@ -19,21 +17,22 @@ public boolean isPalindrome(ListNode head) {
1917
slow = slow.next;
2018
fast = fast.next.next;
2119
}
22-
ListNode cur = slow.next;
20+
ListNode p = slow.next;
2321
slow.next = null;
24-
ListNode pre = null;
25-
while (cur != null) {
26-
ListNode t = cur.next;
27-
cur.next = pre;
28-
pre = cur;
29-
cur = t;
22+
ListNode dummy = new ListNode(0);
23+
while (p != null) {
24+
ListNode next = p.next;
25+
p.next = dummy.next;
26+
dummy.next = p;
27+
p = next;
3028
}
31-
while (pre != null) {
32-
if (pre.val != head.val) {
29+
p = dummy.next;
30+
while (p != null) {
31+
if (head.val != p.val) {
3332
return false;
3433
}
35-
pre = pre.next;
3634
head = head.next;
35+
p = p.next;
3736
}
3837
return true;
3938
}
+15-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val, next) {
4-
* this.val = (val===undefined ? 0 : val)
5-
* this.next = (next===undefined ? null : next)
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
66
* }
77
*/
88
/**
99
* @param {ListNode} head
1010
* @return {boolean}
1111
*/
1212
var isPalindrome = function (head) {
13-
if (!head || !head.next) {
13+
if (!head) {
1414
return true;
1515
}
1616
let slow = head;
@@ -19,21 +19,22 @@ var isPalindrome = function (head) {
1919
slow = slow.next;
2020
fast = fast.next.next;
2121
}
22-
let cur = slow.next;
22+
let p = slow.next;
2323
slow.next = null;
24-
let pre = null;
25-
while (cur) {
26-
let t = cur.next;
27-
cur.next = pre;
28-
pre = cur;
29-
cur = t;
24+
const dummy = new ListNode(0);
25+
while (p) {
26+
const next = p.next;
27+
p.next = dummy.next;
28+
dummy.next = p;
29+
p = next;
3030
}
31-
while (pre) {
32-
if (pre.val !== head.val) {
31+
p = dummy.next;
32+
while (p) {
33+
if (head.val !== p.val) {
3334
return false;
3435
}
35-
pre = pre.next;
3636
head = head.next;
37+
p = p.next;
3738
}
3839
return true;
3940
};
+19-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
66
class Solution:
77
def isPalindrome(self, head: ListNode) -> bool:
8-
if head is None or head.next is None:
8+
if head is None:
99
return True
1010
slow, fast = head, head.next
1111
while fast and fast.next:
12-
slow, fast = slow.next, fast.next.next
13-
pre, cur = None, slow.next
14-
while cur:
15-
t = cur.next
16-
cur.next = pre
17-
pre, cur = cur, t
18-
while pre:
19-
if pre.val != head.val:
12+
slow = slow.next
13+
fast = fast.next.next
14+
p = slow.next
15+
slow.next = None
16+
dummy = ListNode()
17+
while p:
18+
next = p.next
19+
p.next = dummy.next
20+
dummy.next = p
21+
p = next
22+
p = dummy.next
23+
while p:
24+
if head.val != p.val:
2025
return False
21-
pre, head = pre.next, head.next
26+
head = head.next
27+
p = p.next
2228
return True

lcci/02.06.Palindrome Linked List/Solution.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,31 @@
1111
*/
1212

1313
function isPalindrome(head: ListNode | null): boolean {
14-
if (head == null || head.next == null) return true;
15-
// 快慢指针定位到中点
16-
let slow: ListNode = head,
17-
fast: ListNode = head.next;
18-
while (fast != null && fast.next != null) {
14+
if (!head) {
15+
return true;
16+
}
17+
let slow = head;
18+
let fast = head.next;
19+
while (fast && fast.next) {
1920
slow = slow.next;
2021
fast = fast.next.next;
2122
}
22-
// 翻转链表
23-
let cur: ListNode = slow.next;
23+
let p = slow.next;
2424
slow.next = null;
25-
let prev: ListNode = null;
26-
while (cur != null) {
27-
let t: ListNode = cur.next;
28-
cur.next = prev;
29-
prev = cur;
30-
cur = t;
25+
const dummy = new ListNode(0);
26+
while (p) {
27+
const next = p.next;
28+
p.next = dummy.next;
29+
dummy.next = p;
30+
p = next;
3131
}
32-
// 判断回文
33-
while (prev != null) {
34-
if (prev.val != head.val) return false;
35-
prev = prev.next;
32+
p = dummy.next;
33+
while (p) {
34+
if (head.val !== p.val) {
35+
return false;
36+
}
3637
head = head.next;
38+
p = p.next;
3739
}
3840
return true;
3941
}

lcci/02.06.Palindrome Linked List/Solution2.ts

-26
This file was deleted.

lcci/02.08.Linked List Cycle/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>给定一个有环链表,实现一个算法返回环路的开头节点。<br>有环链表的定义:在链表中某个节点的next元素指向在它前面出现过的节点,则表明该链表存在环路。</p><br><p><strong>示例 1:</strong><pre><strong>输入:</strong>head = [3,2,0,-4], pos = 1<br><strong>输出:</strong>tail connects to node index 1<br><strong>解释:</strong>链表中有一个环,其尾部连接到第二个节点。</pre></p><br><p><strong>示例 2:</strong><pre><strong>输入:</strong>head = [1,2], pos = 0<br><strong>输出:</strong>tail connects to node index 0<br><strong>解释:</strong>链表中有一个环,其尾部连接到第一个节点。</pre></p><br><p><strong>示例 3:</strong><pre><strong>输入:</strong>head = [1], pos = -1<br><strong>输出:</strong>no cycle<br><strong>解释:</strong>链表中没有环。</pre></p><br><p><strong>进阶:</strong><br>你是否可以不用额外空间解决此题?</p>
910

1011
## 解法

0 commit comments

Comments
 (0)