Skip to content

Commit

Permalink
reverse linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Mar 20, 2019
1 parent 024f47c commit 79cdb3a
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 206.reverse-linked-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## 题目地址
https://leetcode.com/problems/reverse-linked-list/description/

## 题目描述
Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

## 思路
这个就是常规操作了,使用一个变量记录前驱pre,一个变量记录后继next.

不断更新`current.next = pre` 就好了
## 关键点解析

- 链表的基本操作(交换)
- 虚拟节点dummy 简化操作
- 注意更新current和pre的位置, 否则有可能出现溢出


## 代码

```js
/*
* @lc app=leetcode id=206 lang=javascript
*
* [206] Reverse Linked List
*
* https://leetcode.com/problems/reverse-linked-list/description/
*
* algorithms
* Easy (52.95%)
* Total Accepted: 532.6K
* Total Submissions: 1M
* Testcase Example: '[1,2,3,4,5]'
*
* Reverse a singly linked list.
*
* Example:
*
*
* Input: 1->2->3->4->5->NULL
* Output: 5->4->3->2->1->NULL
*
*
* Follow up:
*
* A linked list can be reversed either iteratively or recursively. Could you
* implement both?
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
const dummyHead = {
next: head
}
let current = dummyHead.next;
let pre = null;

while(current) {
const next = current.next;
current.next = pre;
pre = current;
current = next;
}
return pre;
};

```

142 changes: 142 additions & 0 deletions 92.reverse-linked-list-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
## 题目地址
https://leetcode.com/problems/reverse-linked-list-ii/description/

## 题目描述
Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

## 思路

考虑取出需要反转的这一小段链表,反转完后再插入到原先的链表中。

以本题为例:

变换的是2,3,4这三个点,那么我们可以先取出2,用front指针指向2,然后当取出3的时候,我们把3加到2的前面,把front指针前移到3,依次类推,到4后停止,这样我们得到一个新链表4->3->2, front指针指向4。

对于原链表来说,有两个点的位置很重要,需要用指针记录下来,分别是1和5,把新链表插入的时候需要这两个点的位置。

用pre指针记录1的位置

当4结点被取走后,5的位置需要记下来

这样我们就可以把倒置后的那一小段链表加入到原链表中

![92.reverse-linked-list-ii](./assets/92.reverse-linked-list-ii.gif)

(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
## 关键点解析

- 链表的基本操作(交换)
- 虚拟节点dummy 简化操作
- 考虑特殊情况 m 是 1 或者 n是链表长度的情况
- 用四个变量记录特殊节点, 然后操作这四个节点使之按照一定方式连接即可。

```js
let midStartNode = null;
let preMidStartNode = null;
let midEndNode = null;
let postMidEndNode = null;
```

- 注意更新current和pre的位置, 否则有可能出现溢出


## 代码

```js
/*
* @lc app=leetcode id=92 lang=javascript
*
* [92] Reverse Linked List II
*
* https://leetcode.com/problems/reverse-linked-list-ii/description/
*
* algorithms
* Medium (34.13%)
* Total Accepted: 182.3K
* Total Submissions: 532.8K
* Testcase Example: '[1,2,3,4,5]\n2\n4'
*
* Reverse a linked list from position m to n. Do it in one-pass.
*
* Note: 1 ≤ m ≤ n ≤ length of list.
*
* Example:
*
*
* Input: 1->2->3->4->5->NULL, m = 2, n = 4
* Output: 1->4->3->2->5->NULL
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} m
* @param {number} n
* @return {ListNode}
*/
var reverseBetween = function(head, m, n) {
const dummyHead = {
next: head
}

let current = dummyHead.next;
let pre = current;
let index = 0;

let midStartNode = null;
let preMidStartNode = null;
let midEndNode = null;
let postMidEndNode = null;

while(current) {
const next = current.next;
index++;

// reverse linked list
if (index > m && index <= n) {
current.next = pre;
}

if (index === m - 1) {
preMidStartNode = current;
}
if (index === m) {
midStartNode = current;
}

if (index === n + 1) {
postMidEndNode = current;
postMidEndNode.next
}

if (index === n) {
midEndNode = current;;
}

pre = current;

current = next;
}

// 两个链表合并起来
(preMidStartNode || dummyHead).next = midEndNode; // 特殊情况需要考虑
midStartNode.next = postMidEndNode;

return dummyHead.next;
};

```
Binary file added assets/92.reverse-linked-list-ii.gif
Loading
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 79cdb3a

Please sign in to comment.