mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 13:00:22 +08:00
Merge branch 'master' of https://github.com/cezarbbb/leetcode-master
This commit is contained in:
@ -254,20 +254,19 @@ TypeScript:
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function swapPairs(head: ListNode | null): ListNode | null {
|
function swapPairs(head: ListNode | null): ListNode | null {
|
||||||
const dummyHead: ListNode = new ListNode(0, head);
|
const dummyNode: ListNode = new ListNode(0, head);
|
||||||
let cur: ListNode = dummyHead;
|
let curNode: ListNode | null = dummyNode;
|
||||||
while(cur.next !== null && cur.next.next !== null) {
|
while (curNode && curNode.next && curNode.next.next) {
|
||||||
const tem: ListNode = cur.next;
|
let firstNode: ListNode = curNode.next,
|
||||||
const tem1: ListNode = cur.next.next.next;
|
secNode: ListNode = curNode.next.next,
|
||||||
|
thirdNode: ListNode | null = curNode.next.next.next;
|
||||||
cur.next = cur.next.next; // step 1
|
curNode.next = secNode;
|
||||||
cur.next.next = tem; // step 2
|
secNode.next = firstNode;
|
||||||
cur.next.next.next = tem1; // step 3
|
firstNode.next = thirdNode;
|
||||||
|
curNode = firstNode;
|
||||||
cur = cur.next.next;
|
|
||||||
}
|
}
|
||||||
return dummyHead.next;
|
return dummyNode.next;
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Kotlin:
|
Kotlin:
|
||||||
|
@ -283,6 +283,28 @@ var searchInsert = function (nums, target) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 第一种二分法
|
||||||
|
function searchInsert(nums: number[], target: number): number {
|
||||||
|
const length: number = nums.length;
|
||||||
|
let left: number = 0,
|
||||||
|
right: number = length - 1;
|
||||||
|
while (left <= right) {
|
||||||
|
const mid: number = Math.floor((left + right) / 2);
|
||||||
|
if (nums[mid] < target) {
|
||||||
|
left = mid + 1;
|
||||||
|
} else if (nums[mid] === target) {
|
||||||
|
return mid;
|
||||||
|
} else {
|
||||||
|
right = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return right + 1;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
### Swift
|
### Swift
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
|
@ -659,6 +659,48 @@ func restoreIpAddresses(_ s: String) -> [String] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def restoreIpAddresses(s: String): List[String] = {
|
||||||
|
var result = mutable.ListBuffer[String]()
|
||||||
|
if (s.size < 4 || s.length > 12) return result.toList
|
||||||
|
var path = mutable.ListBuffer[String]()
|
||||||
|
|
||||||
|
// 判断IP中的一个字段是否为正确的
|
||||||
|
def isIP(sub: String): Boolean = {
|
||||||
|
if (sub.size > 1 && sub(0) == '0') return false
|
||||||
|
if (sub.toInt > 255) return false
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
def backtracking(startIndex: Int): Unit = {
|
||||||
|
if (startIndex >= s.size) {
|
||||||
|
if (path.size == 4) {
|
||||||
|
result.append(path.mkString(".")) // mkString方法可以把集合里的数据以指定字符串拼接
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// subString
|
||||||
|
for (i <- startIndex until startIndex + 3 if i < s.size) {
|
||||||
|
var subString = s.substring(startIndex, i + 1)
|
||||||
|
if (isIP(subString)) { // 如果合法则进行下一轮
|
||||||
|
path.append(subString)
|
||||||
|
backtracking(i + 1)
|
||||||
|
path = path.take(path.size - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(0)
|
||||||
|
result.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -584,35 +584,29 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
|
Map<Integer, Integer> map; // 方便根据数值查找位置
|
||||||
public TreeNode buildTree(int[] inorder, int[] postorder) {
|
public TreeNode buildTree(int[] inorder, int[] postorder) {
|
||||||
return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length);
|
map = new HashMap<>();
|
||||||
|
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
|
||||||
|
map.put(inorder[i], i);
|
||||||
}
|
}
|
||||||
public TreeNode buildTree1(int[] inorder, int inLeft, int inRight,
|
|
||||||
int[] postorder, int postLeft, int postRight) {
|
return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开
|
||||||
// 没有元素了
|
}
|
||||||
if (inRight - inLeft < 1) {
|
|
||||||
|
public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
|
||||||
|
// 参数里的范围都是前闭后开
|
||||||
|
if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// 只有一个元素了
|
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
|
||||||
if (inRight - inLeft == 1) {
|
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
|
||||||
return new TreeNode(inorder[inLeft]);
|
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
|
||||||
}
|
root.left = findNode(inorder, inBegin, rootIndex,
|
||||||
// 后序数组postorder里最后一个即为根结点
|
postorder, postBegin, postBegin + lenOfLeft);
|
||||||
int rootVal = postorder[postRight - 1];
|
root.right = findNode(inorder, rootIndex + 1, inEnd,
|
||||||
TreeNode root = new TreeNode(rootVal);
|
postorder, postBegin + lenOfLeft, postEnd - 1);
|
||||||
int rootIndex = 0;
|
|
||||||
// 根据根结点的值找到该值在中序数组inorder里的位置
|
|
||||||
for (int i = inLeft; i < inRight; i++) {
|
|
||||||
if (inorder[i] == rootVal) {
|
|
||||||
rootIndex = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 根据rootIndex划分左右子树
|
|
||||||
root.left = buildTree1(inorder, inLeft, rootIndex,
|
|
||||||
postorder, postLeft, postLeft + (rootIndex - inLeft));
|
|
||||||
root.right = buildTree1(inorder, rootIndex + 1, inRight,
|
|
||||||
postorder, postLeft + (rootIndex - inLeft), postRight - 1);
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -622,31 +616,29 @@ class Solution {
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
|
Map<Integer, Integer> map;
|
||||||
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||||
return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
|
map = new HashMap<>();
|
||||||
|
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
|
||||||
|
map.put(inorder[i], i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TreeNode helper(int[] preorder, int preLeft, int preRight,
|
return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开
|
||||||
int[] inorder, int inLeft, int inRight) {
|
|
||||||
// 递归终止条件
|
|
||||||
if (inLeft > inRight || preLeft > preRight) return null;
|
|
||||||
|
|
||||||
// val 为前序遍历第一个的值,也即是根节点的值
|
|
||||||
// idx 为根据根节点的值来找中序遍历的下标
|
|
||||||
int idx = inLeft, val = preorder[preLeft];
|
|
||||||
TreeNode root = new TreeNode(val);
|
|
||||||
for (int i = inLeft; i <= inRight; i++) {
|
|
||||||
if (inorder[i] == val) {
|
|
||||||
idx = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据 idx 来递归找左右子树
|
public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
|
||||||
root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft),
|
// 参数里的范围都是前闭后开
|
||||||
inorder, inLeft, idx - 1);
|
if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树
|
||||||
root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight,
|
return null;
|
||||||
inorder, idx + 1, inRight);
|
}
|
||||||
|
int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置
|
||||||
|
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
|
||||||
|
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
|
||||||
|
root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
|
||||||
|
inorder, inBegin, rootIndex);
|
||||||
|
root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
|
||||||
|
inorder, rootIndex + 1, inEnd);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -676,5 +676,50 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
|
||||||
|
import scala.collection.mutable
|
||||||
|
|
||||||
|
def partition(s: String): List[List[String]] = {
|
||||||
|
var result = mutable.ListBuffer[List[String]]()
|
||||||
|
var path = mutable.ListBuffer[String]()
|
||||||
|
|
||||||
|
// 判断字符串是否回文
|
||||||
|
def isPalindrome(start: Int, end: Int): Boolean = {
|
||||||
|
var (left, right) = (start, end)
|
||||||
|
while (left < right) {
|
||||||
|
if (s(left) != s(right)) return false
|
||||||
|
left += 1
|
||||||
|
right -= 1
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 回溯算法
|
||||||
|
def backtracking(startIndex: Int): Unit = {
|
||||||
|
if (startIndex >= s.size) {
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 添加循环守卫,如果当前分割是回文子串则进入回溯
|
||||||
|
for (i <- startIndex until s.size if isPalindrome(startIndex, i)) {
|
||||||
|
path.append(s.substring(startIndex, i + 1))
|
||||||
|
backtracking(i + 1)
|
||||||
|
path = path.take(path.size - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(0)
|
||||||
|
result.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
# 141. 环形链表
|
# 141. 环形链表
|
||||||
|
|
||||||
|
[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/)
|
||||||
|
|
||||||
给定一个链表,判断链表中是否有环。
|
给定一个链表,判断链表中是否有环。
|
||||||
|
|
||||||
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
|
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
|
||||||
@ -103,7 +105,7 @@ class Solution:
|
|||||||
return False
|
return False
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func hasCycle(head *ListNode) bool {
|
func hasCycle(head *ListNode) bool {
|
||||||
@ -139,6 +141,23 @@ var hasCycle = function(head) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function hasCycle(head: ListNode | null): boolean {
|
||||||
|
let slowNode: ListNode | null = head,
|
||||||
|
fastNode: ListNode | null = head;
|
||||||
|
while (fastNode !== null && fastNode.next !== null) {
|
||||||
|
slowNode = slowNode!.next;
|
||||||
|
fastNode = fastNode.next.next;
|
||||||
|
if (slowNode === fastNode) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -301,13 +301,13 @@ function detectCycle(head: ListNode | null): ListNode | null {
|
|||||||
let slowNode: ListNode | null = head,
|
let slowNode: ListNode | null = head,
|
||||||
fastNode: ListNode | null = head;
|
fastNode: ListNode | null = head;
|
||||||
while (fastNode !== null && fastNode.next !== null) {
|
while (fastNode !== null && fastNode.next !== null) {
|
||||||
slowNode = (slowNode as ListNode).next;
|
slowNode = slowNode!.next;
|
||||||
fastNode = fastNode.next.next;
|
fastNode = fastNode.next.next;
|
||||||
if (slowNode === fastNode) {
|
if (slowNode === fastNode) {
|
||||||
slowNode = head;
|
slowNode = head;
|
||||||
while (slowNode !== fastNode) {
|
while (slowNode !== fastNode) {
|
||||||
slowNode = (slowNode as ListNode).next;
|
slowNode = slowNode!.next;
|
||||||
fastNode = (fastNode as ListNode).next;
|
fastNode = fastNode!.next;
|
||||||
}
|
}
|
||||||
return slowNode;
|
return slowNode;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
# 143.重排链表
|
# 143.重排链表
|
||||||
|
|
||||||
|
[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/)
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
@ -465,7 +467,81 @@ var reorderList = function(head, s = [], tmp) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TypeScript
|
||||||
|
|
||||||
|
> 辅助数组法:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function reorderList(head: ListNode | null): void {
|
||||||
|
if (head === null) return;
|
||||||
|
const helperArr: ListNode[] = [];
|
||||||
|
let curNode: ListNode | null = head;
|
||||||
|
while (curNode !== null) {
|
||||||
|
helperArr.push(curNode);
|
||||||
|
curNode = curNode.next;
|
||||||
|
}
|
||||||
|
let node: ListNode = head;
|
||||||
|
let left: number = 1,
|
||||||
|
right: number = helperArr.length - 1;
|
||||||
|
let count: number = 0;
|
||||||
|
while (left <= right) {
|
||||||
|
if (count % 2 === 0) {
|
||||||
|
node.next = helperArr[right--];
|
||||||
|
} else {
|
||||||
|
node.next = helperArr[left++];
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
node.next = null;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
> 分割链表法:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function reorderList(head: ListNode | null): void {
|
||||||
|
if (head === null || head.next === null) return;
|
||||||
|
let fastNode: ListNode = head,
|
||||||
|
slowNode: ListNode = head;
|
||||||
|
while (fastNode.next !== null && fastNode.next.next !== null) {
|
||||||
|
slowNode = slowNode.next!;
|
||||||
|
fastNode = fastNode.next.next;
|
||||||
|
}
|
||||||
|
let head1: ListNode | null = head;
|
||||||
|
// 反转后半部分链表
|
||||||
|
let head2: ListNode | null = reverseList(slowNode.next);
|
||||||
|
// 分割链表
|
||||||
|
slowNode.next = null;
|
||||||
|
/**
|
||||||
|
直接在head1链表上进行插入
|
||||||
|
head1 链表长度一定大于或等于head2,
|
||||||
|
因此在下面的循环中,只要head2不为null, head1 一定不为null
|
||||||
|
*/
|
||||||
|
while (head2 !== null) {
|
||||||
|
const tempNode1: ListNode | null = head1!.next,
|
||||||
|
tempNode2: ListNode | null = head2.next;
|
||||||
|
head1!.next = head2;
|
||||||
|
head2.next = tempNode1;
|
||||||
|
head1 = tempNode1;
|
||||||
|
head2 = tempNode2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
function reverseList(head: ListNode | null): ListNode | null {
|
||||||
|
let curNode: ListNode | null = head,
|
||||||
|
preNode: ListNode | null = null;
|
||||||
|
while (curNode !== null) {
|
||||||
|
const tempNode: ListNode | null = curNode.next;
|
||||||
|
curNode.next = preNode;
|
||||||
|
preNode = curNode;
|
||||||
|
curNode = tempNode;
|
||||||
|
}
|
||||||
|
return preNode;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
|
||||||
方法三:反转链表
|
方法三:反转链表
|
||||||
```c
|
```c
|
||||||
//翻转链表
|
//翻转链表
|
||||||
|
@ -465,6 +465,27 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
C#:
|
||||||
|
```csharp
|
||||||
|
public class Solution {
|
||||||
|
public int MinSubArrayLen(int s, int[] nums) {
|
||||||
|
int n = nums.Length;
|
||||||
|
int ans = int.MaxValue;
|
||||||
|
int start = 0, end = 0;
|
||||||
|
int sum = 0;
|
||||||
|
while (end < n) {
|
||||||
|
sum += nums[end];
|
||||||
|
while (sum >= s)
|
||||||
|
{
|
||||||
|
ans = Math.Min(ans, end - start + 1);
|
||||||
|
sum -= nums[start];
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
end++;
|
||||||
|
}
|
||||||
|
return ans == int.MaxValue ? 0 : ans;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -273,7 +273,7 @@ class Solution:
|
|||||||
return pre
|
return pre
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
@ -319,6 +319,63 @@ var isPalindrome = function(head) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TypeScript
|
||||||
|
|
||||||
|
> 数组模拟
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function isPalindrome(head: ListNode | null): boolean {
|
||||||
|
const helperArr: number[] = [];
|
||||||
|
let curNode: ListNode | null = head;
|
||||||
|
while (curNode !== null) {
|
||||||
|
helperArr.push(curNode.val);
|
||||||
|
curNode = curNode.next;
|
||||||
|
}
|
||||||
|
let left: number = 0,
|
||||||
|
right: number = helperArr.length - 1;
|
||||||
|
while (left < right) {
|
||||||
|
if (helperArr[left++] !== helperArr[right--]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
> 反转后半部分链表
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function isPalindrome(head: ListNode | null): boolean {
|
||||||
|
if (head === null || head.next === null) return true;
|
||||||
|
let fastNode: ListNode | null = head,
|
||||||
|
slowNode: ListNode = head,
|
||||||
|
preNode: ListNode = head;
|
||||||
|
while (fastNode !== null && fastNode.next !== null) {
|
||||||
|
preNode = slowNode;
|
||||||
|
slowNode = slowNode.next!;
|
||||||
|
fastNode = fastNode.next.next;
|
||||||
|
}
|
||||||
|
preNode.next = null;
|
||||||
|
let cur1: ListNode | null = head;
|
||||||
|
let cur2: ListNode | null = reverseList(slowNode);
|
||||||
|
while (cur1 !== null) {
|
||||||
|
if (cur1.val !== cur2!.val) return false;
|
||||||
|
cur1 = cur1.next;
|
||||||
|
cur2 = cur2!.next;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
function reverseList(head: ListNode | null): ListNode | null {
|
||||||
|
let curNode: ListNode | null = head,
|
||||||
|
preNode: ListNode | null = null;
|
||||||
|
while (curNode !== null) {
|
||||||
|
let tempNode: ListNode | null = curNode.next;
|
||||||
|
curNode.next = preNode;
|
||||||
|
preNode = curNode;
|
||||||
|
curNode = tempNode;
|
||||||
|
}
|
||||||
|
return preNode;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -190,13 +190,13 @@ javaScript:
|
|||||||
* @return {void} Do not return anything, modify s in-place instead.
|
* @return {void} Do not return anything, modify s in-place instead.
|
||||||
*/
|
*/
|
||||||
var reverseString = function(s) {
|
var reverseString = function(s) {
|
||||||
return s.reverse();
|
//Do not return anything, modify s in-place instead.
|
||||||
|
reverse(s)
|
||||||
};
|
};
|
||||||
|
|
||||||
var reverseString = function(s) {
|
var reverse = function(s) {
|
||||||
let l = -1, r = s.length;
|
let l = -1, r = s.length;
|
||||||
while(++l < --r) [s[l], s[r]] = [s[r], s[l]];
|
while(++l < --r) [s[l], s[r]] = [s[r], s[l]];
|
||||||
return s;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user