mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-11 04:14:41 +00:00
链表专题更新cpp代码
This commit is contained in:
@@ -36,6 +36,8 @@
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public ListNode oddEvenList(ListNode head) {
|
||||
@@ -60,3 +62,30 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* oddEvenList(ListNode* head) {
|
||||
if (head == nullptr || head->next == nullptr) {
|
||||
return head;
|
||||
}
|
||||
ListNode * odd = head;
|
||||
ListNode * even = head->next;
|
||||
ListNode * evenhead = even;
|
||||
|
||||
while (odd->next != nullptr && even->next != nullptr) {
|
||||
//将偶数位合在一起,奇数位合在一起
|
||||
odd->next = even->next;
|
||||
odd = odd->next;
|
||||
even->next = odd->next;
|
||||
even = even->next;
|
||||
}
|
||||
//链接
|
||||
odd->next = evenhead;
|
||||
return head;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user