# [算法]头条面试—奇数位升序偶数位降序链表排序

2018/04/09 23:47

package com.darrenchan.list;

public class OddIncreaseEvenDecrease {
/**
* 按照奇偶位拆分成两个链表
* @return
*/

Node cur1 = null;
Node cur2 = null;
int count = 1;//用来计数
if(count % 2 == 1){
if(cur1 != null){
cur1 = cur1.next;
}else{
}
}else{
if(cur2 != null){
cur2 = cur2.next;
}else{
}
}
count++;
}
//跳出循环，要让最后两个末尾元素的下一个都指向null
cur1.next = null;
cur2.next = null;

return nodes;
}

/**
* 反转链表
* @return
*/
Node pre = null;
Node next = null;
}
return pre;
}

/**
* 合并两个有序链表
* @return
*/
}
Node pre = null;
Node next = null;
while(cur1 != null && cur2 != null){
if(cur1.value <= cur2.value){//这里一定要有=，否则一旦cur1的value和cur2的value相等的话，下面的pre.next会出现空指针异常
pre = cur1;
cur1 = cur1.next;
}else{
next = cur2.next;
pre.next = cur2;
cur2.next = cur1;
pre = cur2;
cur2 = next;
}
}
pre.next = cur1 == null ? cur2 : cur1;

}

public static void main(String[] args) {

}
}

public static Node init(){
Node node1 = new Node(1);
Node node2 = new Node(8);
Node node3 = new Node(3);
Node node4 = new Node(6);
Node node5 = new Node(5);
Node node6 = new Node(4);
Node node7 = new Node(7);
Node node8 = new Node(2);
Node node9 = new Node(9);

node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node7;
node7.next = node8;
node8.next = node9;
return node1;
}
}

public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null)
return null;
if(l1 == null)
return l2;
if(l2 == null)
return l1;
if(l1.val > l2.val)
{
}
else
{
}
}
}

0
0 收藏

0 评论
0 收藏
0