奇偶链表分别放,最后在合并
引入没用的结点充当头
注意:
- 20行、21行的
oddList.next = null ; evenList.next=null
必须要写,因为他们的next不为空
function oddEvenList( head ) {
if(head==null || head.next==null)
return head;
let oddList = {}, savedOddList = oddList;;//奇数链表
let evenList = {}, savedEvenList = evenList;;//偶数链表
let flag = true;//true表示奇数个
while(head != null){
if(flag){
oddList.next = head;
oddList = oddList.next;
}else{
evenList.next = head;
evenList = evenList.next;
}
head = head.next;
flag = !flag;
}
oddList.next = null;
evenList.next = null;//!!!!
oddList.next = savedEvenList.next;
return savedOddList.next;
}
module.exports = {
oddEvenList : oddEvenList
};