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

Commit50a6c68

Browse files
authored
Merge pull requestneetcode-gh#2240 from crazysamurai/add-problem
Create: 0086-partition-list.java
2 parentsbd57f5e +4f16c97 commit50a6c68

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎java/0086-partition-list.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
classSolution {
2+
publicListNodepartition(ListNodehead,intx) {
3+
ListNodehead1 =newListNode();// will store node.value < x
4+
ListNodehead2 =newListNode();// will store node.value >= x
5+
6+
ListNodetail1 =head1,tail2 =head2,curr =head;
7+
8+
while (curr !=null) {
9+
if (curr.val <x) {
10+
tail1.next =curr;
11+
tail1 =tail1.next;
12+
}else {
13+
tail2.next =curr;
14+
tail2 =tail2.next;
15+
}
16+
curr =curr.next;
17+
}
18+
// at this point the two lists are not connected the way we want them to
19+
// we'll need to connect the list with values<x to list with values >= x
20+
// also the last node (tail2) is currently pointing to some random node but it
21+
// should point to null
22+
tail1.next =head2.next;
23+
tail2.next =null;
24+
returnhead1.next;
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp