Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcci problems #2528

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 159 additions & 146 deletions lcci/02.06.Palindrome Linked List/README.md

Large diffs are not rendered by default.

305 changes: 159 additions & 146 deletions lcci/02.06.Palindrome Linked List/README_EN.md

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions lcci/02.06.Palindrome Linked List/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (!head) {
return true;
}
ListNode* slow = head;
ListNode* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* p = slow->next;
slow->next = nullptr;
ListNode* dummy = new ListNode(0);
while (p) {
ListNode* next = p->next;
p->next = dummy->next;
dummy->next = p;
p = next;
}
p = dummy->next;
while (p) {
if (head->val != p->val) {
return false;
}
head = head->next;
p = p->next;
}
return true;
}
};
37 changes: 15 additions & 22 deletions lcci/02.06.Palindrome Linked List/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,37 @@
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public bool IsPalindrome(ListNode head) {
if (head == null || head.next == null)
{
if (head == null) {
return true;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null)
{
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode cur = slow.next;
ListNode p = slow.next;
slow.next = null;
ListNode pre = null;
while (cur != null)
{
ListNode t = cur.next;
cur.next = pre;
pre = cur;
cur = t;
ListNode dummy = new ListNode(0);
while (p != null) {
ListNode next = p.next;
p.next = dummy.next;
dummy.next = p;
p = next;
}
while (pre != null)
{
if (pre.val != head.val)
{
p = dummy.next;
while (p != null) {
if (head.val != p.val) {
return false;
}
pre = pre.next;
head = head.next;
p = p.next;
}
return true;
}
}
}
56 changes: 25 additions & 31 deletions lcci/02.06.Palindrome Linked List/Solution.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func isPalindrome(head *ListNode) bool {
if head == nil {
return true
}
m := mid(head)
temp := reverse(m.Next)
m.Next = nil
p, q := head, temp
res := true
for p != nil && q != nil {
if p.Val != q.Val {
res = false
break
}
p = p.Next
q = q.Next
}
m.Next = reverse(temp)
return res
}

func mid(head *ListNode) *ListNode {
slow, fast := head, head.Next
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
slow, fast = slow.Next, fast.Next.Next
}
return slow
}

func reverse(head *ListNode) *ListNode {
var prev *ListNode = nil
for head != nil {
temp := head.Next
head.Next = prev
prev = head
head = temp
p := slow.Next
slow.Next = nil
dummy := &ListNode{}
for p != nil {
next := p.Next
p.Next = dummy.Next
dummy.Next = p
p = next
}
p = dummy.Next
for p != nil {
if head.Val != p.Val {
return false
}
head = head.Next
p = p.Next
}
return prev
return true
}
27 changes: 13 additions & 14 deletions lcci/02.06.Palindrome Linked List/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
if (head == null) {
return true;
}
ListNode slow = head;
Expand All @@ -19,21 +17,22 @@ public boolean isPalindrome(ListNode head) {
slow = slow.next;
fast = fast.next.next;
}
ListNode cur = slow.next;
ListNode p = slow.next;
slow.next = null;
ListNode pre = null;
while (cur != null) {
ListNode t = cur.next;
cur.next = pre;
pre = cur;
cur = t;
ListNode dummy = new ListNode(0);
while (p != null) {
ListNode next = p.next;
p.next = dummy.next;
dummy.next = p;
p = next;
}
while (pre != null) {
if (pre.val != head.val) {
p = dummy.next;
while (p != null) {
if (head.val != p.val) {
return false;
}
pre = pre.next;
head = head.next;
p = p.next;
}
return true;
}
Expand Down
29 changes: 15 additions & 14 deletions lcci/02.06.Palindrome Linked List/Solution.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function (head) {
if (!head || !head.next) {
if (!head) {
return true;
}
let slow = head;
Expand All @@ -19,21 +19,22 @@ var isPalindrome = function (head) {
slow = slow.next;
fast = fast.next.next;
}
let cur = slow.next;
let p = slow.next;
slow.next = null;
let pre = null;
while (cur) {
let t = cur.next;
cur.next = pre;
pre = cur;
cur = t;
const dummy = new ListNode(0);
while (p) {
const next = p.next;
p.next = dummy.next;
dummy.next = p;
p = next;
}
while (pre) {
if (pre.val !== head.val) {
p = dummy.next;
while (p) {
if (head.val !== p.val) {
return false;
}
pre = pre.next;
head = head.next;
p = p.next;
}
return true;
};
32 changes: 19 additions & 13 deletions lcci/02.06.Palindrome Linked List/Solution.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if head is None or head.next is None:
if head is None:
return True
slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
pre, cur = None, slow.next
while cur:
t = cur.next
cur.next = pre
pre, cur = cur, t
while pre:
if pre.val != head.val:
slow = slow.next
fast = fast.next.next
p = slow.next
slow.next = None
dummy = ListNode()
while p:
next = p.next
p.next = dummy.next
dummy.next = p
p = next
p = dummy.next
while p:
if head.val != p.val:
return False
pre, head = pre.next, head.next
head = head.next
p = p.next
return True
36 changes: 19 additions & 17 deletions lcci/02.06.Palindrome Linked List/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,31 @@
*/

function isPalindrome(head: ListNode | null): boolean {
if (head == null || head.next == null) return true;
// 快慢指针定位到中点
let slow: ListNode = head,
fast: ListNode = head.next;
while (fast != null && fast.next != null) {
if (!head) {
return true;
}
let slow = head;
let fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
// 翻转链表
let cur: ListNode = slow.next;
let p = slow.next;
slow.next = null;
let prev: ListNode = null;
while (cur != null) {
let t: ListNode = cur.next;
cur.next = prev;
prev = cur;
cur = t;
const dummy = new ListNode(0);
while (p) {
const next = p.next;
p.next = dummy.next;
dummy.next = p;
p = next;
}
// 判断回文
while (prev != null) {
if (prev.val != head.val) return false;
prev = prev.next;
p = dummy.next;
while (p) {
if (head.val !== p.val) {
return false;
}
head = head.next;
p = p.next;
}
return true;
}
26 changes: 0 additions & 26 deletions lcci/02.06.Palindrome Linked List/Solution2.ts

This file was deleted.

1 change: 1 addition & 0 deletions lcci/02.08.Linked List Cycle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 题目描述

<!-- 这里写题目描述 -->

<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>

## 解法
Expand Down
Loading
Loading