Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd84764f

Browse files
committed
list
1 parent6e751b5 commitd84764f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎list/Partition.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
packageAlgorithms.list;
2+
3+
importAlgorithms.algorithm.others.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
publicclassPartition {
17+
publicListNodepartition(ListNodehead,intx) {
18+
if (head ==null) {
19+
returnnull;
20+
}
21+
22+
ListNodedummy =newListNode(0);
23+
dummy.next =head;
24+
25+
ListNodepre =dummy;
26+
ListNodecur =head;
27+
28+
// Record the big list.
29+
ListNodebigDummy =newListNode(0);
30+
ListNodebigTail =bigDummy;
31+
32+
while (cur !=null) {
33+
if (cur.val >=x) {
34+
// Unlink the cur;
35+
pre.next =cur.next;
36+
37+
// Add the cur to the tail of the new link.
38+
bigTail.next =cur;
39+
cur.next =null;
40+
41+
// Refresh the bigTail.
42+
bigTail =cur;
43+
44+
// 移除了一个元素的时候,pre不需要修改,因为cur已经移动到下一个位置了。
45+
}else {
46+
pre =pre.next;
47+
}
48+
49+
cur =pre.next;
50+
}
51+
52+
// Link the Big linklist to the smaller one.
53+
pre.next =bigDummy.next;
54+
55+
returndummy.next;
56+
}
57+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp