algorithm-base/animation-simulation/链表篇/leetcode328奇偶链表.md

63 lines
2.5 KiB
Java
Raw Normal View History

2021-03-21 04:10:31 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
### [328. ](https://leetcode-cn.com/problems/odd-even-linked-list/)
####
>
>
> 使 O(1) O(nodes)nodes
1:
> : 1->2->3->4->5->NULL
> : 1->3->5->2->4->NULL
2:
> : 2->1->3->5->6->4->7->NULL
> : 2->3->6->7->1->5->4->NULL
####
#### ****
![](https://img-blog.csdnimg.cn/20210321120150255.gif)
####
```java
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode odd = head;
ListNode even = head.next;
ListNode evenhead = even;
while (odd.next != null && even.next != null) {
//将偶数位合在一起,奇数位合在一起
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
//链接
odd.next = evenhead;
return head;
}
}
```