Skip to content

Commit 3396c78

Browse files
author
luzhicheng
committed
更新md
更新md
1 parent c3efbe9 commit 3396c78

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

leetcode_linked_list/main.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
4. [相交链表](相交链表.md)
77
5. [移除链表元素(两个指针)](移除链表元素.md)
88
6. [反转链表](反转链表.md)
9+
7. [回文链表](回文链表.md)
10+
8. [删除链表中的节点](删除链表中的节点.md)
11+
9. [链表的中间结点](链表的中间结点.md)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 删除链表中的节点
2+
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。**传入函数的唯一参数为 要被删除的节点**
3+
4+
* 链表至少包含两个节点。
5+
* 链表中所有节点的值都是唯一的。
6+
* 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
7+
* 不要从你的函数中返回任何结果。
8+
9+
#### 示例
10+
```python
11+
输入:head = [4,5,1,9], node = 5
12+
输出:[4,1,9]
13+
解释:给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
14+
```
15+
16+
#### 代码
17+
与之前做过的一个题目很像,[移除链表元素(两个指针)](移除链表元素.md)
18+
但之前做的题目的参数包括链表本身,以及要删除的目标元素,可以通过迭代链表,通过前继节点与当前节点两个索引来完成。
19+
20+
本题目只知道唯一参数是要被删除的节点。
21+
我们将要删除节点的 next 节点的值赋值给要删除的节点,转而去删除 next 节点,从而达成目的。
22+
23+
```python
24+
4->5->1->9 node = 5
25+
# node.val = node.next.val
26+
4->1->1->9
27+
# node.next = node.next.next
28+
4->1->9
29+
```
30+
31+
```python
32+
def deleteNode(node):
33+
node.val = node.next.val
34+
node.next = node.next.next
35+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## 回文链表
2+
请判断一个链表是否为回文链表。
3+
#### 示例
4+
```python
5+
输入: 1->2->2->1
6+
输出: true
7+
输入: 1->2
8+
输出: false
9+
```
10+
11+
#### 代码
12+
把链表转成列表,通过列表判断是否回文
13+
```python
14+
def isPalindrome(head):
15+
val_list = []
16+
cur = head
17+
while cur:
18+
val_list.append(cur.val)
19+
cur = cur.next
20+
return val_list == val_list[::-1]
21+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 链表的中间结点
2+
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
3+
4+
如果有两个中间结点,则返回第二个中间结点。
5+
6+
#### 示例
7+
```python
8+
输入:[1,2,3,4,5]
9+
输出:此列表中的结点 3 (序列化形式:[3,4,5])
10+
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
11+
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
12+
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
13+
```
14+
15+
#### 代码
16+
**两次遍历**
17+
18+
* 第一次遍历链表,得到链表长度,计算中间节点的位置
19+
* 第二次遍历链表,到中间节点就返回
20+
21+
```python
22+
def middleNode(self, head: ListNode) -> ListNode:
23+
count = 0
24+
cur = head
25+
while cur:
26+
count += 1
27+
cur = cur.next
28+
29+
mid = int(count/2 + 1)
30+
31+
count = 0
32+
cur = head
33+
while cur:
34+
count += 1
35+
if count == mid:
36+
return cur
37+
cur = cur.next
38+
```
39+
40+
**一次遍历**
41+
```python
42+
def middleNode(head):
43+
A = [head]
44+
while A[-1].next:
45+
A.append(A[-1].next)
46+
mid = len(A)//2
47+
return A[mid]
48+
```
49+
50+
**快慢指针**
51+
用两个指针slow与fast一起遍历链表。slow 一次走一步,fast一次走两步。那么当fast到达链表的末尾时,slow必然位于中间。
52+
53+
```python
54+
def middleNode(head):
55+
slow = head
56+
fast = head
57+
while fast and fast.next:
58+
slow = slow.next
59+
fast = fast.next.next
60+
return slow
61+
```

0 commit comments

Comments
 (0)