File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ ## :pencil2 : 基础刷题之(92. Reverse Linked List) II)
3
+ <br >.
4
+ ** 2020-01-29 吴亲库里 库里的深夜食堂**
5
+
6
+ ### :pencil2 : 描述
7
+ ** 从指定位置开始到指定位置结束反转着中间的链表。**
8
+ ### :pencil2 : 题目实例
9
+
10
+ <a href =" https://github.com/wuqinqiang/ " >
11
+ <img src =" https://github.com/wuqinqiang/Lettcode-php/blob/master/images/92.png " >
12
+ </a >
13
+
14
+ ****
15
+ ### :pencil2 : 题目分析
16
+
17
+ ** 像这类题目有可能头结点也会发生变动,但是给定的链表是没有固定的头指针的,所以我们一般会自己创建一个 dummy 的 node。这样的话即使头结点变动了,我们只需要使用 dummy->next 即可。我们需要找到反转开始的前一个节点,因为反转结束之后我们需要把它和前面的链表连接起来。解题的方式有很多种,我这里主要列下代码执行的过程变化。链表类的题目最好自己动手。**
18
+ ``` php
19
+ 1 -> 2 -> 3 -> 4 -> 5 -> NULL
20
+
21
+ 1 -> 3 -> 2 -> 4 -> 5 -> NULL
22
+
23
+ 1 -> 4 -> 3 -> 2 -> 5 -> NULL
24
+ ```
25
+ ### :pencil2 : 最终实现代码
26
+
27
+ ``` php
28
+
29
+ **
30
+ * Definition for a singly-linked list.
31
+ * class ListNode {
32
+ * public $val = 0;
33
+ * public $next = null;
34
+ * function __construct($val) { $this->val = $val; }
35
+ * }
36
+ */
37
+ class Solution {
38
+
39
+ /**
40
+ * @param ListNode $head
41
+ * @param Integer $m
42
+ * @param Integer $n
43
+ * @return ListNode
44
+ */
45
+ function reverseBetween($head, $m, $n) {
46
+ $dummy=new ListNode(-1);
47
+ $pre=$dummy;
48
+ $dummy->next=$head;
49
+ for($i=0;$i<$m-1;$i++){
50
+ $pre=$pre->next;
51
+ }
52
+ $cur=$pre->next;
53
+ for($i=$m;$i<$n;$i++){
54
+ $t=$cur->next;
55
+ $cur->next=$t->next;
56
+ $t->next=$pre->next;
57
+ $pre->next=$t;
58
+ }
59
+ return $dummy->next;
60
+ }
61
+ }
62
+
63
+ ```
64
+
65
+ #### 联系
66
+
67
+ <a href =" https://github.com/wuqinqiang/ " >
68
+ <img src =" https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg " width =" 150px " height =" 150px " >
69
+ </a >
Original file line number Diff line number Diff line change 88
88
- [ Leetcode82] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/82.md )
89
89
- [ Leetcode86] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/86.md )
90
90
- [ Leetcode88] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/88.md )
91
+ - [ Leetcode92] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/92.md )
91
92
- [ Leetcode93] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/93.md )
92
93
- [ Leetcode94] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/94.md )
93
94
- [ Leetcode96] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/96.md )
You can’t perform that action at this time.
0 commit comments