LeetCode【147.对链表进行插入排序】

题目描述:

对链表进行插入排序。
在这里插入图片描述
插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。每次迭代时,从输入数据中移除一个元素,并原地将其插入已拍好的链表中。
示例 1

  • 输入 4 − > 2 − > 1 − > 3 4->2->1->3 4>2>1>3
  • 输出 1 − > 2 − > 3 − > 4 1->2->3->4 1>2>3>4

示例 2

  • 输入 − 1 − > 5 − > 3 − > 4 − > 0 -1->5->3->4->0 1>5>3>4>0
  • 输出 − 1 − > 0 − > 3 − > 4 − > 5 -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(N2)
  • 空间复杂度 O ( 1 ) 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));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值