题解 | #链表的奇偶重排#
链表的奇偶重排
http://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
/**
* so easy
* @param head
* @return
*/
public static ListNode oddEvenList(ListNode head) {
// write code here
if (head == null) {
return null;
}
ListNode t1 = head;
ListNode t2 = head.next;
ListNode tail = t1;
ListNode cur = head;
ListNode old = head.next;
while (old != null) {
ListNode node = old.next;
if (node == null) {
break;
}
ListNode node1 = node.next;
cur.next = node;
cur = node;
old.next = node1;
old = node1;
tail = node;
}
tail.next = t2;
return t1;
} 算法 文章被收录于专栏
数据结构和算法