Skip to content

Commit d893c31

Browse files
committed
Add solution 328 [Java]
1 parent 4516e84 commit d893c31

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

README.md

+6-12
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
</p>
1212

1313
## Introduction
14-
Complete solutions to Leetcode problems, updated daily.
14+
Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to Leetcode problems, [updated daily](https://github.com/doocs/leetcode/projects/1).
1515

1616
## Solution List
1717

18-
<detail>
19-
<summary><b>Easy</b></summary>
18+
### Easy
2019

2120
| # | Title | Tags |
2221
|---|---|---|
@@ -41,11 +40,8 @@ Complete solutions to Leetcode problems, updated daily.
4140
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
4241
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
4342

44-
</detail>
4543

46-
47-
<detail>
48-
<summary><b>Medium</b></summary>
44+
### Medium
4945

5046
| # | Title | Tags |
5147
|---|---|---|
@@ -71,11 +67,10 @@ Complete solutions to Leetcode problems, updated daily.
7167
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
7268
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
7369
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
70+
| 328 | [Odd Even Linked List](https://github.com/doocs/leetcode/tree/master/solution/328.Odd%20Even%20Linked%20List) | `Linked List` |
7471

75-
</detail>
7672

77-
<detail>
78-
<summary><b>Hard</b></summary>
73+
### Hard
7974

8075
| # | Title | Tags |
8176
|---|---|---|
@@ -85,10 +80,9 @@ Complete solutions to Leetcode problems, updated daily.
8580
| 145 | [Binary Tree Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
8681
| 295 | [Find Median from Data Stream](https://github.com/doocs/leetcode/tree/master/solution/295.Find%20Median%20from%20Data%20Stream) | `Heap`, `Design` |
8782

88-
</detail>
8983

9084
## Contributions
91-
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
85+
I'm looking for long-term contributors/partners to this repo! Send me [PRs](https://github.com/doocs/leetcode/pulls) if you're interested! See the following:
9286
- Fork this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
9387
- Submit a pull request with your changes!
9488

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 奇偶链表
2+
### 题目描述
3+
4+
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。
5+
6+
请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。
7+
8+
**示例 1:**
9+
```
10+
输入: 1->2->3->4->5->NULL
11+
输出: 1->3->5->2->4->NULL
12+
```
13+
14+
**示例 2:**
15+
```
16+
输入: 2->1->3->5->6->4->7->NULL
17+
输出: 2->3->6->7->1->5->4->NULL
18+
```
19+
20+
**说明:**
21+
22+
- 应当保持奇数节点和偶数节点的相对顺序。
23+
- 链表的第一个节点视为奇数节点,第二个节点视为偶数节点,以此类推。
24+
25+
### 解法
26+
利用 `pre`, `cur`指针指向链表的第 1、2 个结点,循环:将 `cur` 的下一个结点挂在 `pre` 的后面,`pre``cur` 指针向右移动。
27+
28+
eg.
29+
```
30+
1->2->3->4->5: `pre`指向 1,`cur`指向 2
31+
第一次循环后:1->3->2->4->5: `pre`指向 2, `cur`指向 4
32+
...
33+
```
34+
35+
```java
36+
/**
37+
* Definition for singly-linked list.
38+
* public class ListNode {
39+
* int val;
40+
* ListNode next;
41+
* ListNode(int x) { val = x; }
42+
* }
43+
*/
44+
class Solution {
45+
public ListNode oddEvenList(ListNode head) {
46+
// 链表结点数小于 3,直接返回
47+
if (head == null || head.next == null || head.next.next == null) {
48+
return head;
49+
}
50+
ListNode dummy = new ListNode(-1);
51+
dummy.next = head;
52+
ListNode pre = head, t = pre.next, cur = t;
53+
while (cur != null && cur.next != null) {
54+
pre.next = cur.next;
55+
cur.next = cur.next.next;
56+
pre.next.next = t;
57+
pre = pre.next;
58+
// cur.next可能为空,所以在下一次循环要判断 cur!= null 是否满足
59+
cur = cur.next;
60+
t = pre.next;
61+
}
62+
63+
return dummy.next;
64+
}
65+
}
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode oddEvenList(ListNode head) {
11+
// 链表结点数小于 3,直接返回
12+
if (head == null || head.next == null || head.next.next == null) {
13+
return head;
14+
}
15+
ListNode dummy = new ListNode(-1);
16+
dummy.next = head;
17+
ListNode pre = head, t = pre.next, cur = t;
18+
while (cur != null && cur.next != null) {
19+
pre.next = cur.next;
20+
cur.next = cur.next.next;
21+
pre.next.next = t;
22+
pre = pre.next;
23+
// cur.next可能为空,所以在下一次循环要判断 cur != null 是否满足
24+
cur = cur.next;
25+
t = pre.next;
26+
}
27+
28+
return dummy.next;
29+
}
30+
}

0 commit comments

Comments
 (0)