zoukankan      html  css  js  c++  java
  • [leetcode] Insertion Sort List

    Sort a linked list using insertion sort.

    https://oj.leetcode.com/problems/insertion-sort-list/

    思路:模拟插入排序,因为没有pre的指针,所以找位置是从前向后找的。

    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if (head == null || head.next == null)
                return head;
            ListNode newHead = new ListNode(-1);
            newHead.next = head;
    
            ListNode cur = head.next;
            ListNode pre = head;
            while (cur != null) {
                ListNode next = cur.next;
                ListNode p = newHead;
                while (p.next != null && p.next.val < cur.val)
                    p = p.next;
    
                pre.next = next;
                cur.next = p.next;
                p.next = cur;
                if (p == pre)
                    pre = pre.next;
    
                cur = next;
            }
    
            return newHead.next;
        }
    
        public static void main(String[] args) {
            ListNode head = ListUtils.makeList(new int[] { 1, 3, 5, 6, 4 });
            ListUtils.printList(head);
            head = new Solution().insertionSortList(head);
            ListUtils.printList(head);
    
        }
    
    }

    第二遍记录:之前的做法是直接在原有链表上修改,有点复杂,代码可读性太差。。

    修改下实现:新建一个开始dummyHead点,然后依次将原有链表上的元素加进去,每次添加的时候要从新的表头开始扫描到插入的位置。

    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if(head == null)
                return null;
            ListNode helper = new ListNode(0);
            ListNode pre = helper;
            ListNode cur = head;
            while(cur!=null)
            {
                ListNode next = cur.next;
                pre = helper;
                while(pre.next!=null && pre.next.val<cur.val)
                {
                    pre = pre.next;
                }
                cur.next = pre.next;
                pre.next = cur;
                cur = next;
            }
            return helper.next;
        }
        public static void main(String[] args) {
            ListNode head = ListUtils.makeList(new int[] { 1, 2, 4, 3, 5 });
            ListUtils.printList(head);
            head = new Solution().insertionSortList(head);
            ListUtils.printList(head);
    
        }
    
    }

    第三遍:

    思路忘记了, 第二遍的思路,新建一个newHead节点,注意这次不跟head连接! newHead后面挂载已经排好序的, 在原链表上依次取节点在newHead中寻找she和插入的位置的插进去。

    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if (head == null || head.next == null)
                return head;
    
            ListNode newHead = new ListNode(-1);
            ListNode pre = newHead;
    
            ListNode cur = head;
    
            while (cur != null) {
                ListNode post = cur.next;
                pre = newHead;
                while (pre.next != null && pre.next.val < cur.val) {
                    pre = pre.next;
                }
    
                cur.next = pre.next;
                pre.next = cur;
    
                cur = post;
            }
    
            return newHead.next;
    
        }
    
        public static void main(String[] args) {
            ListNode head = ListUtils.makeList(new int[] { 1, 3, 2, 4 });
            ListUtils.printList(head);
            head = new Solution().insertionSortList(head);
            ListUtils.printList(head);
    
        }
    
    }

    参考:

    http://blog.csdn.net/linhuanmars/article/details/21144553

  • 相关阅读:
    jquery-ui.min.js:5 Uncaught TypeError: b.nodeName.toLowerCase is not a function
    HTTP::Request
    LWP::UserAgent
    perl json模块
    perl 处理perl返回的json
    perl中 wx返回的json需要encode_utf8($d);
    perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset
    encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.
    为什么出现Wide character in print at a14.pl line 41
    perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3830452.html
Copyright © 2011-2022 走看看