- 奇偶链表
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。
请尝试使用原地算法完成。你的算法的空间复杂度应为 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
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null){
return head;
}
ListNode j = head;
head = head.next;
ListNode o = head;
head = head.next;
ListNode tj = j;
ListNode to = o;
boolean f = true;
while (head != null){
if (f){
tj.next = head;
tj = tj.next;
f = false;
}else {
to.next = head;
to = to.next;
f = true;
}
head = head.next;
}
tj.next = o;
to.next = null;
return j;
}
}