Skip to content

Commit 36196ce

Browse files
authored
增加目录
1 parent 3481546 commit 36196ce

File tree

1 file changed

+131
-107
lines changed

1 file changed

+131
-107
lines changed

数据结构与算法/Leetcode-LinkList1.md

Lines changed: 131 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
<!-- MarkdownTOC -->
2+
3+
- [1. 两数相加](#1-两数相加)
4+
- [题目描述](#题目描述)
5+
- [问题分析](#问题分析)
6+
- [Solution](#solution)
7+
- [2. 翻转链表](#2-翻转链表)
8+
- [题目描述](#题目描述-1)
9+
- [问题分析](#问题分析-1)
10+
- [Solution](#solution-1)
11+
- [3. 链表中倒数第k个节点](#3-链表中倒数第k个节点)
12+
- [题目描述](#题目描述-2)
13+
- [问题分析](#问题分析-2)
14+
- [Solution](#solution-2)
15+
- [4. 删除链表的倒数第N个节点](#4-删除链表的倒数第n个节点)
16+
- [问题分析](#问题分析-3)
17+
- [Solution](#solution-3)
18+
- [5. 合并两个排序的链表](#5-合并两个排序的链表)
19+
- [题目描述](#题目描述-3)
20+
- [问题分析](#问题分析-4)
21+
- [Solution](#solution-4)
22+
23+
<!-- /MarkdownTOC -->
24+
125

226
# 1. 两数相加
327

@@ -85,12 +109,12 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
85109

86110
```java
87111
public class ListNode {
88-
int val;
89-
ListNode next = null;
112+
int val;
113+
ListNode next = null;
90114

91-
ListNode(int val) {
92-
this.val = val;
93-
}
115+
ListNode(int val) {
116+
this.val = val;
117+
}
94118
}
95119
```
96120

@@ -103,47 +127,47 @@ public class ListNode {
103127
*/
104128
public class Solution {
105129

106-
public ListNode ReverseList(ListNode head) {
107-
108-
ListNode next = null;
109-
ListNode pre = null;
130+
public ListNode ReverseList(ListNode head) {
110131

111-
while (head != null) {
112-
// 保存要反转到头的那个节点
113-
next = head.next;
114-
// 要反转的那个节点指向已经反转的上一个节点(备注:第一次反转的时候会指向null)
115-
head.next = pre;
116-
// 上一个已经反转到头部的节点
117-
pre = head;
118-
// 一直向链表尾走
119-
head = next;
120-
}
121-
return pre;
122-
}
132+
ListNode next = null;
133+
ListNode pre = null;
134+
135+
while (head != null) {
136+
// 保存要反转到头的那个节点
137+
next = head.next;
138+
// 要反转的那个节点指向已经反转的上一个节点(备注:第一次反转的时候会指向null)
139+
head.next = pre;
140+
// 上一个已经反转到头部的节点
141+
pre = head;
142+
// 一直向链表尾走
143+
head = next;
144+
}
145+
return pre;
146+
}
123147

124148
}
125149
```
126150

127151
测试方法:
128152

129153
```java
130-
public static void main(String[] args) {
131-
132-
ListNode a = new ListNode(1);
133-
ListNode b = new ListNode(2);
134-
ListNode c = new ListNode(3);
135-
ListNode d = new ListNode(4);
136-
ListNode e = new ListNode(5);
137-
a.next = b;
138-
b.next = c;
139-
c.next = d;
140-
d.next = e;
141-
new Solution().ReverseList(a);
142-
while (e != null) {
143-
System.out.println(e.val);
144-
e = e.next;
145-
}
146-
}
154+
public static void main(String[] args) {
155+
156+
ListNode a = new ListNode(1);
157+
ListNode b = new ListNode(2);
158+
ListNode c = new ListNode(3);
159+
ListNode d = new ListNode(4);
160+
ListNode e = new ListNode(5);
161+
a.next = b;
162+
b.next = c;
163+
c.next = d;
164+
d.next = e;
165+
new Solution().ReverseList(a);
166+
while (e != null) {
167+
System.out.println(e.val);
168+
e = e.next;
169+
}
170+
}
147171
```
148172

149173
输出:
@@ -185,33 +209,33 @@ public class ListNode {
185209
// 时间复杂度O(n),一次遍历即可
186210
// https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
187211
public class Solution {
188-
public ListNode FindKthToTail(ListNode head, int k) {
189-
// 如果链表为空或者k小于等于0
190-
if (head == null || k <= 0) {
191-
return null;
192-
}
193-
// 声明两个指向头结点的节点
194-
ListNode node1 = head, node2 = head;
195-
// 记录节点的个数
196-
int count = 0;
197-
// 记录k值,后面要使用
198-
int index = k;
199-
// p指针先跑,并且记录节点数,当node1节点跑了k-1个节点后,node2节点开始跑,
200-
// 当node1节点跑到最后时,node2节点所指的节点就是倒数第k个节点
201-
while (node1 != null) {
202-
node1 = node1.next;
203-
count++;
204-
if (k < 1 && node1 != null) {
205-
node2 = node2.next;
206-
}
207-
k--;
208-
}
209-
// 如果节点个数小于所求的倒数第k个节点,则返回空
210-
if (count < index)
211-
return null;
212-
return node2;
213-
214-
}
212+
public ListNode FindKthToTail(ListNode head, int k) {
213+
// 如果链表为空或者k小于等于0
214+
if (head == null || k <= 0) {
215+
return null;
216+
}
217+
// 声明两个指向头结点的节点
218+
ListNode node1 = head, node2 = head;
219+
// 记录节点的个数
220+
int count = 0;
221+
// 记录k值,后面要使用
222+
int index = k;
223+
// p指针先跑,并且记录节点数,当node1节点跑了k-1个节点后,node2节点开始跑,
224+
// 当node1节点跑到最后时,node2节点所指的节点就是倒数第k个节点
225+
while (node1 != null) {
226+
node1 = node1.next;
227+
count++;
228+
if (k < 1 && node1 != null) {
229+
node2 = node2.next;
230+
}
231+
k--;
232+
}
233+
// 如果节点个数小于所求的倒数第k个节点,则返回空
234+
if (count < index)
235+
return null;
236+
return node2;
237+
238+
}
215239
}
216240
```
217241

@@ -264,29 +288,29 @@ public class Solution {
264288
*/
265289
// https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/
266290
public class Solution {
267-
public ListNode removeNthFromEnd(ListNode head, int n) {
268-
// 哑结点,哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部
269-
ListNode dummy = new ListNode(0);
270-
// 哑结点指向头结点
271-
dummy.next = head;
272-
// 保存链表长度
273-
int length = 0;
274-
ListNode len = head;
275-
while (len != null) {
276-
length++;
277-
len = len.next;
278-
}
279-
length = length - n;
280-
ListNode target = dummy;
281-
// 找到 L-n 位置的节点
282-
while (length > 0) {
283-
target = target.next;
284-
length--;
285-
}
286-
// 把第 (L - n)个结点的 next 指针重新链接至第 (L - n + 2)个结点
287-
target.next = target.next.next;
288-
return dummy.next;
289-
}
291+
public ListNode removeNthFromEnd(ListNode head, int n) {
292+
// 哑结点,哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部
293+
ListNode dummy = new ListNode(0);
294+
// 哑结点指向头结点
295+
dummy.next = head;
296+
// 保存链表长度
297+
int length = 0;
298+
ListNode len = head;
299+
while (len != null) {
300+
length++;
301+
len = len.next;
302+
}
303+
length = length - n;
304+
ListNode target = dummy;
305+
// 找到 L-n 位置的节点
306+
while (length > 0) {
307+
target = target.next;
308+
length--;
309+
}
310+
// 把第 (L - n)个结点的 next 指针重新链接至第 (L - n + 2)个结点
311+
target.next = target.next.next;
312+
return dummy.next;
313+
}
290314
}
291315
```
292316

@@ -314,28 +338,28 @@ public class Solution {
314338
* }
315339
*/
316340
public class Solution {
317-
public ListNode removeNthFromEnd(ListNode head, int n) {
318-
319-
ListNode dummy = new ListNode(0);
320-
dummy.next = head;
321-
// 声明两个指向头结点的节点
322-
ListNode node1 = dummy, node2 = dummy;
323-
324-
// node1 节点先跑,node1节点 跑到第 n 个节点的时候,node2 节点开始跑
325-
// 当node1 节点跑到最后一个节点时,node2 节点所在的位置就是第 (L-n ) 个节点,也就是倒数第 n+1(L代表总链表长度)
326-
while (node1 != null) {
327-
node1 = node1.next;
328-
if (n < 1 && node1 != null) {
329-
node2 = node2.next;
330-
}
331-
n--;
332-
}
341+
public ListNode removeNthFromEnd(ListNode head, int n) {
342+
343+
ListNode dummy = new ListNode(0);
344+
dummy.next = head;
345+
// 声明两个指向头结点的节点
346+
ListNode node1 = dummy, node2 = dummy;
347+
348+
// node1 节点先跑,node1节点 跑到第 n 个节点的时候,node2 节点开始跑
349+
// 当node1 节点跑到最后一个节点时,node2 节点所在的位置就是第 (L-n ) 个节点,也就是倒数第 n+1(L代表总链表长度)
350+
while (node1 != null) {
351+
node1 = node1.next;
352+
if (n < 1 && node1 != null) {
353+
node2 = node2.next;
354+
}
355+
n--;
356+
}
333357

334-
node2.next = node2.next.next;
358+
node2.next = node2.next.next;
335359

336-
return dummy.next;
360+
return dummy.next;
337361

338-
}
362+
}
339363
}
340364
```
341365

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy