/**
* 单链表奇数递增偶数递减,使之升序
* 分三步:
* 1.拆分成2个链表
* 2.对逆序的链表反转
* 3.合并2个链表
*/
public class SortedOddAndEvenList {
public static class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}
// 这一步注意细节
public static ListNode[] getTwoList(ListNode head) {
if (head == null || head.next == null) return null;
ListNode l1 = head;
ListNode l2 = head.next;
ListNode next = null;
ListNode copyNode = null;
while (l1 != null) {
next = l1.next.next;
copyNode = l1.next;
l1.next = next;
copyNode.next = next == null ? null : next.next;
l1 = next;
}
return new ListNode[]{head, l2};
}
public static ListNode reverse(ListNode head) {
ListNode pre = null, next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static ListNode mergeListNode(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null) p.next = l1;
if (l2 != null) p.next = l2;
return dummy.next;
}
public static void main(String[] args) {
ListNode head = new ListNode(0);
head.next = new ListNode(9);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(8);
ListNode[] twoList = getTwoList(head);
if (twoList == null) {
return;
}
ListNode l1 = twoList[0];
ListNode l2 = reverse(twoList[1]);
ListNode res = mergeListNode(l1, l2);
while (res != null) {
System.out.println(res.val);
res = res.next;
}
}
}
单链表奇数递增偶数递减,使之升序
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1二进制中1的个数 【题目】输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。 【考察点】位运算 【...
- 去面试问了单链表实现快排的问题,所以想来把八大排序算法的单链表实现总结一下。 这篇就先总结直接插入排序。实际上,算...
- 问题:给定单链表中某一结点node,但不给链表的头结点。如果删除node?要求时间复杂度为O(n)。 解法:用待删...