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

Commite304095

Browse files
author
applewjg
committed
PartitionList
Change-Id: I40932199bacbaa3ad9806f67047eca948d8f508f
1 parent9f73ef6 commite304095

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

‎PartitionList.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Author: King, higuige@gmail.com
3+
Date: Oct 7, 2014
4+
Problem: Partition List
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/partition-list/
7+
Notes:
8+
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
9+
You should preserve the original relative order of the nodes in each of the two partitions.
10+
For example,
11+
Given 1->4->3->2->5->2 and x = 3,
12+
return 1->2->2->4->3->5.
13+
14+
Solution: ...
15+
*/
16+
17+
/**
18+
* Definition for singly-linked list.
19+
* public class ListNode {
20+
* int val;
21+
* ListNode next;
22+
* ListNode(int x) {
23+
* val = x;
24+
* next = null;
25+
* }
26+
* }
27+
*/
28+
publicclassSolution {
29+
publicListNodepartition_1(ListNodehead,intx) {
30+
ListNodeleftdummy =newListNode(-1);
31+
ListNoderightdummy =newListNode(-1);
32+
ListNodelhead =leftdummy;
33+
ListNoderhead =rightdummy;
34+
35+
for(ListNodecur =head;cur !=null;cur=cur.next){
36+
if(cur.val <x){
37+
lhead.next =cur;
38+
lhead =lhead.next;
39+
}else{
40+
rhead.next =cur;
41+
rhead =rhead.next;
42+
}
43+
}
44+
lhead.next =rightdummy.next;
45+
rhead.next =null;
46+
returnleftdummy.next;
47+
}
48+
publicListNodepartition(ListNodehead,intx) {
49+
ListNodedummy =newListNode(-1);
50+
dummy.next =head;
51+
ListNodecur =dummy;
52+
ListNodeins =dummy;
53+
while (cur.next !=null &&cur.next.val <x) {
54+
cur =cur.next;
55+
ins =ins.next;
56+
}
57+
while (cur.next !=null) {
58+
if (cur.next.val >=x) {
59+
cur =cur.next;
60+
}else {
61+
ListNodenode =cur.next;
62+
cur.next =cur.next.next;
63+
node.next =ins.next;
64+
ins.next =node;
65+
ins =ins.next;
66+
}
67+
}
68+
returndummy.next;
69+
}
70+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp