Skip to content

Commit

Permalink
feat: 优化js代码,新增链表相关刷题文档
Browse files Browse the repository at this point in the history
  • Loading branch information
Chu Fan committed Mar 26, 2023
1 parent 3c0e1d6 commit d99044f
Show file tree
Hide file tree
Showing 28 changed files with 76 additions and 85 deletions.
8 changes: 4 additions & 4 deletions code/algorithm/剑指/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@

- [ ] [【简单】数组中出现次数超过一半的数字](./数学/moreThanHalfNum.md)
- [ ] [【中等】圆圈中最后剩下的数 约瑟夫问题](./数学/LastRemaining_Solution.md)
- [ ] [【中等】从1到n整数中1出现的次数](./数学/NumberOf1Between1AndN_Solution.js)
- [ ] [【中等】从1到n整数中1出现的次数](./数学/numberOf1Between1AndN.js)

#### 位运算

- [ ] [【中等】二进制中1的个数](./位运算/NumberOf1.js)
- [ ] [【中等】二进制中1的个数](./位运算/numberOf1.js)
- [ ] [【中等】数组中只出现一次的数字]()

#### 其他分类

- [ ] [【简单】不用加减乘除做加法](./其他相关/add.js)
- [ ] [【中等】扑克牌顺子](./其他相关/IsContinuous.js)
- [ ] [【较难】把字符串转换成整数](./其他相关/StrToInt.js)
- [ ] [【中等】扑克牌顺子](./其他相关/isContinuous.js)
- [ ] [【较难】把字符串转换成整数](./其他相关/strToInt.js)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 39 additions & 22 deletions code/algorithm/剑指/链表/printListFromTailToHead.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
/*
* @Description: 【较难】从尾到头打印链表
* @Description: 【简单】从尾到头打印链表
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2021-05-01 20:49:28
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-05-01 21:24:40
*/

/**
* 单链表数据结构
* @param x
* @constructor
*/
function ListNode(x) {
this.val = x
this.next = null
}

// 偷懒写法
function printListFromTailToHead(head) {
// 只有单个指针,从头出链表,按序放入数组,最后翻转数组
/**
* 从头出链表,按序放入数组,最后翻转数组
* 偷懒写法
* @param head
* @returns {*[]}
*/
function printListFromTailToHeadOne(head) {
const result = []
while (head !== null) {
result.push(head.val)
Expand All @@ -25,10 +34,32 @@ function printListFromTailToHead(head) {
return result.reverse()
}

// 先翻转链表【采用头插法】,再按序输入到数组中
function printListFromTailToHead01(head) {

/**
* 相比链表的头插,对数组array进行头插unshift()即可
* @param head
* @returns {*[]}
*/
function printListFromTailToHeadTwo(head) {
const result = []
while (head !== null) {
// 头插
result.unshift(head.val)
// 下一个结点
head = head.next
}
return result
}


/**
* 先翻转链表【采用头插法】,再按序输出到数组中
* @param head
* @returns {*[]}
*/
function printListFromTailToHeadThree(head) {
let reverseHead = new ListNode(-1)
// 头插法
// 翻转链表
while (head !== null) {
const pre = head
// 下一个结点
Expand All @@ -38,27 +69,13 @@ function printListFromTailToHead01(head) {
}
// 重新整理 去掉val=-1的点
reverseHead = reverseHead.next

const result = []
// 遍历链表
while (reverseHead !== null) {
result.push(reverseHead.val)
// 下一个结点
reverseHead = reverseHead.next
}
// 返回
return result
}

// 相比链表的头插,这里对数组array进行头插unshift()即可
function printListFromTailToHead02(head) {
const result = []
while (head !== null) {
result.unshift(head.val)

// 下一个结点
head = head.next
}

// 返回,输出
return result
}
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/manuscript/solo-algorithm/sword-point/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- [ ] [【简单】两个链表的第一个公共结点](链表/findFirstCommonNode.md)
- [ ] [【中等】链表中倒数第K个结点](链表/findKthToTail.md)
- [ ] [【中等】反转链表](链表/reverseList.md)
- [ ] [较难】从尾到头打印链表](链表/printListFromTailToHead.md)
- [x] [简单】从尾到头打印链表](链表/printListFromTailToHead.md)
- ~~在O(1)时间内删除链表节点~~
- [ ] [【较难】删除链表中重复的结点](链表/deleteDuplication.md)
- [ ] 链表中环的入口结点[暂时没思路]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,38 @@
/*
* @Description: 【较难】从尾到头打印链表
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2021-05-01 20:49:28
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-05-01 21:24:40
*/

function ListNode(x) {
this.val = x
this.next = null
}
# 从尾到头打印链表

// 偷懒写法
function printListFromTailToHead(head) {
// 只有单个指针,从头出链表,按序放入数组,最后翻转数组
const result = []
while (head !== null) {
result.push(head.val)
// 下一个元素
head = head.next
}
// 翻转并返回
return result.reverse()
}
### 题目链接

// 先翻转链表【采用头插法】,再按序输入到数组中
function printListFromTailToHead01(head) {
let reverseHead = new ListNode(-1)
// 头插法
while (head !== null) {
const pre = head
// 下一个结点
head = head.next
pre.next = reverseHead.next
reverseHead.next = pre
}
// 重新整理 去掉val=-1的点
reverseHead = reverseHead.next
const result = []
// 遍历链表
while (reverseHead !== null) {
result.push(reverseHead.val)
// 下一个结点
reverseHead = reverseHead.next
}
// 返回
return result
}
- [牛客网](https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035)
- [欢迎讨论]()

### 题目描述

输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。

如输入{1,2,3}的链表如下图:

![](./printListFromTailToHead.png)

返回一个数组为[3,2,1]

// 相比链表的头插,这里对数组array进行头插unshift()即可
function printListFromTailToHead02(head) {
const result = []
while (head !== null) {
result.unshift(head.val)
0 <= 链表长度 <= 10000

// 下一个结点
head = head.next
}
### 思路

// 返回,输出
return result
单链表比较直接的想法就是:顺讯访问、按照链表结点循环即可,比较好的模型:

```js

while(head!=null){
// 结点向后
head=head.next
}
```


### 代码实现

@[code js](@code/algorithm/剑指/链表/printListFromTailToHead.js)


### 一些建议
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d99044f

Please sign in to comment.