這道題可以分成三步:
首先根據奇數字和偶數字拆分成兩個鍊錶。
然後對偶數鍊錶進行反轉。
最後將兩個有序鍊錶進行合併(合併兩種實現方式一種是遞迴另外一種是非遞迴)。
class
node
}
public
static
void
main
(string[
] args)
}
public
static node init()
public
static node[
]getlists
(node head)
else
}else
else
} count++
; head = head.next;
} cur1.next = null;
cur2.next = null;
node[
] list =
newnode
;return list;
}
public
static node reverselist
(node head)
return pre;
}
public
static node combinelist
(node head1, node head2)
node head = head1.value < head2.value ? head1 : head2;
node cur1 = head == head1 ? head1 : head2;
node cur2 = head == head1 ? head2 : head1;
node pre = null;
node next = null;
while
(cur1 != null && cur2 != null)
else
}
pre.next = cur1 == null ? cur2 : cur1;
return head;
}
public
static node mergetwolists
(node l1, node l2)
node head = null;
if(l1.value > l2.value)
else
return head;
}
演算法 頭條面試 奇數字公升序偶數字降序鍊錶排序
題目描述 乙個鍊錶,奇數字公升序偶數字降序,讓鍊錶變成公升序的。比如 1 8 3 6 5 4 7 2 9,最後輸出1 2 3 4 5 6 7 8 9。分析 這道題可以分成三步 首先根據奇數字和偶數字拆分成兩個鍊錶。然後對偶數鍊錶進行反轉。最後將兩個有序鍊錶進行合併。package com.darre...
涉及陣列奇數字 偶數字的問題 資料終極狀態問題
有一類問題很巧,這類問題涉及到在原陣列的奇數字 偶數字之間來回移動資料,類似題目有力扣893.groups of special equivalent strings與力扣1217.minimum cost to move chips to the same position 巧在什麼地方呢,先看1...
奇數字於偶數的前半部分
輸入乙個整數陣列,實現乙個函式來調整該陣列中數字的順序,使得所有的奇數字於陣列的前半部分,所有的偶數字於陣列的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。兩個思路吧,第乙個思路 類似冒泡演算法,前偶後奇數就交換 class solution 第二個思路 再建立乙個陣列 class so...