Skip to content

Commit d3bc74c

Browse files
committed
添加py和js
1 parent 474937e commit d3bc74c

File tree

1 file changed

+69
-23
lines changed

1 file changed

+69
-23
lines changed

animation-simulation/链表篇/leetcode86分隔链表.md

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,20 @@
3737
Java Code:
3838

3939
```java
40-
/**
41-
* Definition for singly-linked list.
42-
* public class ListNode {
43-
* int val;
44-
* ListNode next;
45-
* ListNode(int x) { val = x; }
46-
* }
47-
*/
4840
class Solution {
4941
public ListNode partition(ListNode head, int x) {
50-
if (head == null) {
51-
return head;
52-
}
5342
ListNode pro = head;
5443
ListNode big = new ListNode(-1);
5544
ListNode small = new ListNode(-1);
5645
ListNode headbig = big;
57-
ListNode headsmall =small;
58-
//
59-
while (pro != null) {
46+
ListNode headsmall = small;
47+
//
48+
while (pro != null) {
6049
//大于时,放到 big 链表上
6150
if (pro.val >= x) {
6251
big.next = pro;
6352
big = big.next;
64-
// 小于放到 small 链表上
53+
//小于时,放到 small 链表上
6554
}else {
6655
small.next = pro;
6756
small = small.next;
@@ -83,21 +72,18 @@ C++ Code:
8372
class Solution {
8473
public:
8574
ListNode* partition(ListNode* head, int x) {
86-
if (head == nullptr) {
87-
return head;
88-
}
8975
ListNode * pro = head;
9076
ListNode * big = new ListNode(-1);
9177
ListNode * small = new ListNode(-1);
9278
ListNode * headbig = big;
93-
ListNode * headsmall =small;
94-
//分
95-
while (pro != nullptr) {
79+
ListNode * headsmall = small;
80+
//分
81+
while (pro != nullptr) {
9682
//大于时,放到 big 链表上
9783
if (pro->val >= x) {
9884
big->next = pro;
9985
big = big->next;
100-
// 小于放到 small 链表上
86+
//小于时,放到 small 链表上
10187
}else {
10288
small->next = pro;
10389
small = small->next;
@@ -113,5 +99,65 @@ public:
11399
};
114100
```
115101
102+
JS Code:
103+
104+
```js
105+
var partition = function(head, x) {
106+
let pro = head;
107+
let big = new ListNode(-1);
108+
let small = new ListNode(-1);
109+
let headbig = big;
110+
let headsmall = small;
111+
//分
112+
while (pro) {
113+
//大于时,放到 big 链表上
114+
if (pro.val >= x) {
115+
big.next = pro;
116+
big = big.next;
117+
//小于时,放到 small 链表上
118+
}else {
119+
small.next = pro;
120+
small = small.next;
121+
}
122+
pro = pro.next;
123+
}
124+
//细节
125+
big.next = null;
126+
//合
127+
small.next = headbig.next;
128+
return headsmall.next;
129+
};
130+
```
116131

117-
132+
Python Code:
133+
134+
```py
135+
# Definition for singly-linked list.
136+
# class ListNode:
137+
# def __init__(self, val=0, next=None):
138+
# self.val = val
139+
# self.next = next
140+
class Solution:
141+
def partition(self, head: ListNode, x: int) -> ListNode:
142+
pro = head
143+
big = ListNode(-1)
144+
small = ListNode(-1)
145+
headbig = big
146+
headsmall = small
147+
#
148+
while pro is not None:
149+
# 大于时,放到 big 链表上
150+
if pro.val >= x:
151+
big.next = pro
152+
big = big.next
153+
# 小于时,放到 small 链表上
154+
else:
155+
small.next = pro
156+
small = small.next
157+
pro = pro.next
158+
# 细节
159+
big.next = None
160+
#
161+
small.next = headbig.next
162+
return headsmall.next
163+
```

0 commit comments

Comments
 (0)