一、概述
有一个链表,奇数位升序偶数位降序,如何将链表变成升序。
二、思路
1)将链表根据奇偶顺序拆成两个链表
2)将偶数位拆成的链表反转
3)将两个链表合并
三、代码
private static ListNode[] splitList(ListNode head) { ListNode cur = head; ListNode head1 = null; ListNode head2 = null; ListNode cur1 = null; ListNode cur2 = null; int num = 1; while (head != null) { if (num % 2 == 1) { if (cur1 != null) { cur1.next = head; cur1 = cur1.next; } else { cur1 = head; head1 = cur1; } } else { if (cur2 != null) { cur2.next = head; cur2 = cur2.next; } else { cur2 = head; head2 = cur2; } } head = head.next; num++; } cur1.next = null; cur2.next = null; ListNode[] heads = new ListNode[]{head1, head2}; return heads; } private