Skip to content

Commit 00cafe0

Browse files
committed
note
1 parent 2616bfa commit 00cafe0

File tree

6 files changed

+202
-9
lines changed

6 files changed

+202
-9
lines changed

guangxu/202012/20201202.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
## Algorithm
22

3+
[206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
4+
35
### Description
46

5-
### Solution
7+
Reverse a singly linked list.
8+
9+
Example:
610

7-
```java
11+
```
12+
Input: 1->2->3->4->5->NULL
13+
Output: 5->4->3->2->1->NULL
14+
```
15+
16+
Follow up:
17+
18+
A linked list can be reversed either iteratively or recursively. Could you implement both?
19+
20+
### Solution
821

22+
```java
23+
/**
24+
* Definition for singly-linked list.
25+
* public class ListNode {
26+
* int val;
27+
* ListNode next;
28+
* ListNode() {}
29+
* ListNode(int val) { this.val = val; }
30+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
31+
* }
32+
*/
33+
class Solution {
34+
public ListNode reverseList(ListNode head){
35+
if(head == null || head.next == null){
36+
return head;
37+
}
38+
ListNode pre = null;
39+
ListNode now = head;
40+
while(now!=null){
41+
ListNode next = now.next;
42+
now.next = pre;
43+
pre = now;
44+
now = next;
45+
}
46+
return pre;
47+
}
48+
}
949
```
1050

1151
### Discuss

guangxu/202012/20201203.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,85 @@
11
## Algorithm
22

3+
[234. Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)
4+
35
### Description
46

5-
### Solution
7+
Given a singly linked list, determine if it is a palindrome.
68

7-
```java
9+
Example 1:
10+
```
11+
Input: 1->2
12+
Output: false
13+
```
814

15+
Example 2:
16+
```
17+
Input: 1->2->2->1
18+
Output: true
19+
```
20+
21+
Follow up:
22+
- Could you do it in O(n) time and O(1) space?
23+
24+
### Solution
25+
26+
```java
27+
/**
28+
* Definition for singly-linked list.
29+
* public class ListNode {
30+
* int val;
31+
* ListNode next;
32+
* ListNode() {}
33+
* ListNode(int val) { this.val = val; }
34+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
35+
* }
36+
*/
37+
class Solution {
38+
private ListNode reverseList(ListNode head){
39+
ListNode pre = null;
40+
ListNode now = head;
41+
while(now!=null){
42+
ListNode next = now.next;
43+
now.next = pre;
44+
pre = now;
45+
now = next;
46+
}
47+
return pre;
48+
}
49+
private boolean isEqual(ListNode h1, ListNode h2) {
50+
while(h1!=null&&h2!=null){
51+
if(h1.val!=h2.val){
52+
return false;
53+
}
54+
h1 = h1.next;
55+
h2 = h2.next;
56+
}
57+
return true;
58+
}
59+
public boolean isPalindrome(ListNode head) {
60+
if(head == null || head.next==null){
61+
return true;
62+
}
63+
ListNode slow = head;
64+
ListNode fast = head;
65+
while(fast!=null&&fast.next!=null&&fast.next.next!=null){
66+
slow = slow.next;
67+
fast = fast.next.next;
68+
}
69+
ListNode firstHalfHead = head;
70+
ListNode firstHalfTail = slow;
71+
ListNode secondHalfHead = reverseList(firstHalfTail.next);
72+
boolean result = isEqual(firstHalfHead, secondHalfHead);
73+
reverseList(firstHalfTail.next);
74+
return result;
75+
}
76+
}
977
```
1078

1179
### Discuss
1280

81+
找中点+链表反转+遍历是否相等
82+
1383
## Review
1484

1585

guangxu/202012/20201204.md

+83-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,96 @@
11
## Algorithm
22

3+
[237. Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)
4+
35
### Description
46

5-
### Solution
7+
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
8+
9+
It is guaranteed that the node to be deleted is not a tail node in the list.
10+
11+
Example 1:
12+
13+
![](assets/20201204-02e2e166.png)
14+
15+
```
16+
Input: head = [4,5,1,9], node = 5
17+
Output: [4,1,9]
18+
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
19+
```
20+
21+
Example 2:
22+
23+
![](assets/20201204-59644db6.png)
24+
25+
```
26+
Input: head = [4,5,1,9], node = 1
27+
Output: [4,5,9]
28+
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
29+
```
30+
31+
Example 3:
32+
33+
```
34+
Input: head = [1,2,3,4], node = 3
35+
Output: [1,2,4]
36+
```
637

7-
```java
38+
Example 4:
839

940
```
41+
Input: head = [0,1], node = 0
42+
Output: [1]
43+
```
44+
45+
Example 5:
46+
47+
```
48+
Input: head = [-3,5,-99], node = -3
49+
Output: [5,-99]
50+
```
51+
52+
Constraints:
53+
54+
- The number of the nodes in the given list is in the range [2, 1000].
55+
- -1000 <= Node.val <= 1000
56+
- The value of each node in the list is unique.
57+
- The node to be deleted is in the list and is not a tail node
58+
59+
### Solution
60+
61+
```java
62+
/**
63+
* Definition for singly-linked list.
64+
* public class ListNode {
65+
* int val;
66+
* ListNode next;
67+
* ListNode(int x) { val = x; }
68+
* }
69+
*/
70+
class Solution {
71+
public void deleteNode(ListNode node) {
72+
ListNode curr = node;
73+
ListNode nxt = node.next;
74+
if(nxt != null){
75+
curr.val = nxt.val;
76+
curr.next = nxt.next;
77+
}
78+
}
79+
}
80+
```
1081

1182
### Discuss
1283

84+
这道题完全没有给除了目标节点以外的任何参数,必须通过修改val的值来实现删除节点。
85+
86+
1. 把node.next的值赋给node
87+
88+
2. 把node.next指向node.next.next
89+
90+
其实相当于删除node.next
91+
92+
93+
1394
## Review
1495

1596

43.6 KB
Loading
44.7 KB
Loading

guangxu/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| :-----| :---- | :---- | :---- | :---- |
3-
| [20201201](./202012/20201201.md) |[141. Linked List Cycle(判断链表中是否有环)](https://leetcode.com/problems/linked-list-cycle/)
4-
|||
3+
| [20201204](./202012/20201204.md) |[237. Delete Node in a Linked List(删除链表中的一个节点)](https://leetcode.com/problems/delete-node-in-a-linked-list/)|||
4+
| [20201203](./202012/20201203.md) |[234. Palindrome Linked List(回文链表)](https://leetcode.com/problems/palindrome-linked-list/)|||
5+
| [20201202](./202012/20201202.md) |[206. Reverse Linked List(链表反转)](https://leetcode.com/problems/reverse-linked-list/)|||
6+
| [20201201](./202012/20201201.md) |[141. Linked List Cycle(判断链表中是否有环)](https://leetcode.com/problems/linked-list-cycle/)|||
57
| [20201130](./202011/20201130.md) |[142. Linked List Cycle II(找链表环的交点)](https://leetcode.com/problems/linked-list-cycle-ii/)|||
68
| [20201129](./202011/20201129.md) |[146. LRU Cache](https://leetcode.com/problems/lru-cache/)|||
79
| [20201128](./202011/20201128.md) |[199. Binary Tree Right Side View (二叉树右视图)](https://leetcode.com/problems/binary-tree-right-side-view/)|||
@@ -24,7 +26,7 @@
2426
| [20201111](./202011/20201111.md) |[103. Binary Tree Zigzag Level Order Traversal(之字形打印二叉树)](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)|||
2527
| [20201110](./202011/20201110.md) |[102. Binary Tree Level Order Traversal(二叉树层次遍历)](https://leetcode.com/problems/binary-tree-level-order-traversal/)|||
2628
| [20201109](./202011/20201109.md) |[239. Sliding Window Maximum(滑动窗口最大值)](https://leetcode.com/problems/sliding-window-maximum/)|||
27-
| [20201108](./202011/20201108.md) |[72. Edit Distance](https://leetcode.com/problems/edit-distance/)|||
29+
| [20201108](./202011/20201108.md) |[72. Edit Distance(编辑距离)](https://leetcode.com/problems/edit-distance/)|||
2830
| [20201107](./202011/20201107.md) |[5. Longest Palindromic Substring(最长回文串)](https://leetcode.com/problems/longest-palindromic-substring/)|希尔排序||
2931
| [20201106](./202011/20201106.md) |[69. Sqrt(x)(折半查找求根号)](https://leetcode.com/problems/sqrtx/)|选择排序||
3032
| [20201105](./202011/20201105.md) |[62. Unique Paths(不同路径)](https://leetcode.com/problems/unique-paths/)|插入排序|||

0 commit comments

Comments
 (0)