链表专题更新cpp代码

This commit is contained in:
3119005212
2021-04-28 18:28:00 +08:00
parent 6d96954aa7
commit afd452aeda
15 changed files with 627 additions and 12 deletions

View File

@@ -34,6 +34,8 @@
**题目代码**
Java Code:
```java
/**
* Definition for singly-linked list.
@@ -75,7 +77,41 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (head == nullptr) {
return head;
}
ListNode * pro = head;
ListNode * big = new ListNode(-1);
ListNode * small = new ListNode(-1);
ListNode * headbig = big;
ListNode * headsmall =small;
//分
while (pro != nullptr) {
//大于时,放到 big 链表上
if (pro->val >= x) {
big->next = pro;
big = big->next;
// 小于放到 small 链表上
}else {
small->next = pro;
small = small->next;
}
pro = pro->next;
}
//细节
big->next = nullptr;
//合
small->next = headbig->next;
return headsmall->next;
}
};
```