数据库升序降序查询代码(降序和升序的区别)
作者
一、概述
有一个链表数据库升序降序查询代码,奇数位升序偶数位降序数据库升序降序查询代码,如何将链表变成升序。
二、思路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 static ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while (cur != null) { ListNode next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre;}private static ListNode mergeLists(ListNode head1, ListNode head2) { if (head1 == null && head2 == null) { return null; } if (head1 == null || head2 == null) { return head1 == null ? head2 : head1; } ListNode first = new ListNode(-1); ListNode cur = first; while (head1 != null && head2 != null) { if (head1.val < head2.val) { cur.next = head1; head1 = head1.next; } else { cur.next = head2; head2 = head2.next; } } cur.next = head1 == null ? head2 : head1; return first.next;}目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~