mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 13:03:41 +00:00
添加py和js,去除多于代码,补全代码缺漏
This commit is contained in:
parent
f7c6fe7abf
commit
b043dc0786
@ -168,30 +168,28 @@ class Solution {
|
||||
ListNode midenode = searchmidnode(head);
|
||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
|
||||
ListNode backhalf = reverse(midenode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode p1 = head;
|
||||
ListNode p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
midenode.next = reverse(backhalf);
|
||||
return false;
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
// 还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midenode.next = reverse(backhalf);
|
||||
return true;
|
||||
}
|
||||
//找到中间的部分
|
||||
public ListNode searchmidnode (ListNode head) {
|
||||
ListNode fast = new ListNode(-1);
|
||||
ListNode slow = new ListNode(-1);
|
||||
fast = head;
|
||||
slow = head;
|
||||
//找到中点
|
||||
public ListNode searchmidnode (ListNode head) {
|
||||
ListNode fast = head;
|
||||
ListNode slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
@ -202,7 +200,6 @@ class Solution {
|
||||
public ListNode reverse (ListNode slow) {
|
||||
ListNode low = null;
|
||||
ListNode temp = null;
|
||||
//翻转链表
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
@ -223,35 +220,33 @@ public:
|
||||
if (head == nullptr || head->next == nullptr) {
|
||||
return true;
|
||||
}
|
||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||
ListNode * midenode = searchmidnode(head);
|
||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
//这里我们用的是midnode->next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
|
||||
ListNode * backhalf = reverse(midenode->next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode * p1 = head;
|
||||
ListNode * p2 = backhalf;
|
||||
while (p2 != nullptr) {
|
||||
if (p1->val != p2->val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
midenode.next = reverse(backhalf);
|
||||
return false;
|
||||
}
|
||||
p1 = p1->next;
|
||||
p2 = p2->next;
|
||||
}
|
||||
// 还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midenode->next = reverse(backhalf);
|
||||
return true;
|
||||
}
|
||||
//找到中间的部分
|
||||
ListNode * searchmidnode (ListNode * head) {
|
||||
ListNode * fast = new ListNode(-1);
|
||||
ListNode * slow = new ListNode(-1);
|
||||
fast = head;
|
||||
slow = head;
|
||||
//找到中点
|
||||
ListNode * fast = head;
|
||||
ListNode * slow = head;
|
||||
while (fast->next != nullptr && fast->next->next != nullptr) {
|
||||
fast = fast->next->next;
|
||||
slow = slow->next;
|
||||
@ -262,7 +257,6 @@ public:
|
||||
ListNode * reverse (ListNode * slow) {
|
||||
ListNode * low = nullptr;
|
||||
ListNode * temp = nullptr;
|
||||
//翻转链表
|
||||
while (slow != nullptr) {
|
||||
temp = slow->next;
|
||||
slow->next = low;
|
||||
@ -274,3 +268,109 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var isPalindrome = function(head) {
|
||||
if (head === null || head.next === null) {
|
||||
return true;
|
||||
}
|
||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||
let midenode = searchmidnode(head);
|
||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
let backhalf = reverse(midenode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
let p1 = head;
|
||||
let p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
midenode.next = reverse(backhalf);
|
||||
return false;
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midenode.next = reverse(backhalf);
|
||||
return true;
|
||||
};
|
||||
|
||||
//找到中点
|
||||
var searchmidnode = function(head) {
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return slow;
|
||||
};
|
||||
|
||||
//翻转链表
|
||||
var reverse = function(slow) {
|
||||
let low = null;
|
||||
let temp = null;
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
low = slow;
|
||||
slow = temp;
|
||||
}
|
||||
return low;
|
||||
};
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def isPalindrome(self, head: ListNode) -> bool:
|
||||
if head is None or head.next is None:
|
||||
return True
|
||||
# 找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
# 但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||
midnode = self.searchmidnode(head)
|
||||
# 原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
# 这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
backhalf = self.reverse(midnode.next)
|
||||
# 遍历两部分链表,判断值是否相等
|
||||
p1 = head
|
||||
p2 = backhalf
|
||||
while p2 is not None:
|
||||
if p1.val != p2.val:
|
||||
# 若要还原,记得这里也要reverse
|
||||
midnode.next = self.reverse(backhalf)
|
||||
print(head)
|
||||
return False
|
||||
p1 = p1.next
|
||||
p2 = p2.next
|
||||
# 还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode.next = self.reverse(backhalf)
|
||||
return True
|
||||
|
||||
# 找到中点
|
||||
def searchmidnode(self, head):
|
||||
fast = head
|
||||
slow = head
|
||||
while fast.next is not None and fast.next.next is not None:
|
||||
fast = fast.next.next
|
||||
slow = slow.next
|
||||
return slow
|
||||
|
||||
# 翻转链表
|
||||
def reverse(self, slow):
|
||||
low = None
|
||||
temp = None
|
||||
while slow is not None:
|
||||
temp = slow.next
|
||||
slow.next = low
|
||||
low = slow
|
||||
slow = temp
|
||||
return low
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user