-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path328.奇偶链表.java
37 lines (35 loc) · 942 Bytes
/
328.奇偶链表.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* @lc app=leetcode.cn id=328 lang=java
*
* [328] 奇偶链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head == null) {
return head;
}
// need to store evenHead, after the while, head.next will change and
// may not be head of even List
ListNode odd = head, even = head.next, evenHead = even;
while(odd.next != null && odd.next.next != null) {
odd.next = odd.next.next;
odd = odd.next;
even.next = even.next.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
// @lc code=end