1. 程式人生 > >leetcode【147.對連結串列進行插入排序】

leetcode【147.對連結串列進行插入排序】

題目描述:

對連結串列進行插入排序。
在這裡插入圖片描述
插入排序的動畫演示如上。從第一個元素開始,該連結串列可以被認為已經部分排序(用黑色表示)。每次迭代時,從輸入資料中移除一個元素,並原地將其插入已拍好的連結串列中。
示例 1

  • 輸入 4 > 2
    > 1 > 3 4->2->1->3
  • 輸出 1
    > 2 > 3 > 4
    1->2->3->4

示例 2

  • 輸入 1 > 5 > 3 > 4 > 0 -1->5->3->4->0
  • 輸出 1 > 0 > 3 > 4 > 5 -1->0->3->4->5

思路
利用插入排序的思想,每次移動一個元素,至已排序的連結串列中合適的位置。直到資料遍歷完為止。重複操作。
程式碼

class Solution {
    public ListNode insertionSortList(ListNode head) {
      ListNode cur = new ListNode(-1);
        ListNode dummpy = cur;
        ListNode prev = head;
        while(head != null){
            ListNode cur2 = cur;
            prev = head;
            head = head.next;
            while(cur2.next != null && cur2.next.val <= prev.val) {
                cur2 = cur2.next;
                //System.out.println(cur2.val);
            }
            //cur2.next = prev;
            prev.next = cur2.next;
            cur2.next = prev;
        }
        return dummpy.next;
    }
}

複雜度分析

  • 時間複雜度 O ( N 2 ) O(N^2)
  • 空間複雜度 O ( 1 ) O(1)

完整程式碼

package leetcode147;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 張超帥 on 2018/10/19.
 */
class ListNode {
    int val;
    ListNode next;
    ListNode(int x){this.val = x;}
}
class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode cur = new ListNode(-1);
        ListNode dummpy = cur;
        ListNode prev = head;
        while(head != null){
            ListNode cur2 = cur;
            prev = head;
            head = head.next;
            while(cur2.next != null && cur2.next.val <= prev.val) {
                cur2 = cur2.next;
            }
            prev.next = cur2.next;
            cur2.next = prev;
        }
        return dummpy.next;
    }
}
public class leetcode147 {
    public static int[] stringToArrays(String input){
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if(input == null) {
            return new int[0];
        }
        String[] parts = input.split(",");
        int[] res = new int[parts.length];
        for(int i = 0; i < parts.length; i ++) {
            res[i] = Integer.parseInt(parts[i]);
        }
        return res;
    }
    public static ListNode stringToListNode(String input) {
        int[] nodes = stringToArrays( input);
        ListNode dummpy = new ListNode(-1);
        ListNode cur = dummpy;
        for(int node : nodes){
            cur.next = new ListNode(node);
            cur = cur.next;
        }
        return dummpy.next;

    }
    public static String listnodeToString(ListNode head){
        if(head == null){
            return "[]";
        }
        String res = "";
        while(head != null){
            res += head.val + ", ";
            head = head.next;
        }
        return "[" + res.substring(0, res.length() - 2) + "]";
    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line = in.readLine()) != null) {
            ListNode head = stringToListNode(line);
            ListNode ret = new Solution().insertionSortList(head);
            System.out.println(listnodeToString(ret));
        }
    }
}