【编程】链表排序问题

来源:互联网 发布:水平很高的网络词 编辑:程序博客网 时间:2024/06/01 23:21

1。单向链表的选择排序。时间复杂度O(n^2)空间复杂度O(1)
(1)方法一 可以交换链表的值,不改变链表的指向。
(2)方法二 找到原链表值最小的节点,删掉并放在新链表的末尾。
2。Sort a linked list in O(n log n) time using constant space complexity.
利用归并对链表进行排序。题目链接:148. Sort List
代码中实现的是方法二。

import java.util.Scanner;public class SortListDemo {    public static class ListNode {        public int val;        public ListNode next;        public ListNode(int data) {            this.val = data;            next = null;        }    }    /**     * 头插法创建链表,手动输入     */    public static ListNode createLinkedList() {        ListNode head = null;        Scanner scanner = new Scanner(System.in);        while(scanner.hasNext()) {            int reader = scanner.nextInt();            ListNode newListNode = new ListNode(reader);            newListNode.next = head;            head = newListNode;        }        return head;    }    /**     * 遍历元素     */    public static void travelLinkedList(ListNode head) {        ListNode pnext = head;        while(pnext != null) {            System.out.print(pnext.val + "  ");            pnext = pnext.next;        }        System.out.println();    }    /**     *      * @param head     * @return 选择排序好的head     */    public static ListNode selectSort(ListNode head) {        ListNode tail = null;        ListNode cur= head;        ListNode smallPre = null;        ListNode small= null;        while(cur != null)         {            small = cur;            smallPre = getSmallestPreListNode(cur);            if(smallPre != null) {                small = smallPre.next;                smallPre.next = small.next;            }            cur = (cur == small) ? cur.next:cur;            if(tail == null) {                head = small;            } else {                tail.next = small;            }            tail = small;        }        return head;    }    public static ListNode getSmallestPreListNode(ListNode head) {        ListNode smallPre = null;        ListNode small = head;        ListNode pre = head;        ListNode cur = head.next;        while(cur != null) {            if(cur.val < small.val) {                smallPre = pre;                small = cur;            }            pre = cur;            cur = cur.next;        }        return smallPre;    }    /**     * 链表归并排序     * 快慢指针,获取链表的中间指针     * @param head     * @return     */    public static ListNode MegeSort(ListNode head) {             if(head == null || head.next == null){                 return head;             }             ListNode fast = head;           ListNode slow = head;             while(fast != null && fast.next != null && fast.next.next != null){                 fast = fast.next.next;                 slow = slow.next;             }             fast = slow;             slow = slow.next;             fast.next = null;             fast = MegeSort(head);             slow = MegeSort(slow);             return merge(fast, slow);          }          /**         * 归并两个有序链表         * @param sub1         * @param sub2         * @return         */        private  static ListNode merge(ListNode sub1, ListNode sub2)        {            if(sub1== null){                  return sub2;              }              if(sub2 == null){                  return sub1;              }            ListNode head;              if(sub1.val < sub2.val){                  head = sub1;                  sub1 = sub1.next;              }else{                  head = sub2;                  sub2 = sub2.next;              }              ListNode p = head;            while(sub1 != null && sub2 != null){                  if(sub1.val < sub2.val){                      p.next = sub1;                      sub1 = sub1.next;                  }else{                      p.next = sub2;                      sub2 = sub2.next;                  }                  p = p.next;              }              if(sub1 != null){                  p.next = sub1;              }              if(sub2 != null){                  p.next = sub2;              }              return head;          }        public static void main(String[] args) {            ListNode head = createLinkedList();            //排序前输出            travelLinkedList(head);            //ListNode newHead = selectSort(head);            ListNode newHead = MegeSort(head);            //排序后输出            travelLinkedList(newHead);        }}