2019.7.20刷题记录

Sort a linked list in O(n log n) time using constant space complexity.Sort a linked list in O(n log n) time using constant space complexity.Sort a linked list in O(n log n) time using constant space complexity.要求时间复杂度O(nlogn) 空间复杂度O(1),考虑归并排序

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode mid = getMid(head);
        ListNode next = mid.next;
        mid.next = null;
        return sort(sortList(head) , sortList(next));//将两个已经有序的再排序
    }
    public ListNode getMid(ListNode node){
        if(node == null || node.next == null){
            return node;
        }
        //快慢指针找中间节点
        ListNode fast = node;
        ListNode slow = node;
        while(slow.next != null && fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode sort(ListNode n1 , ListNode n2){
        ListNode Head = new ListNode(0);
        ListNode cur = Head;
        ListNode cur1 = n1;
        ListNode cur2 = n2;
        while(cur1 != null && cur2 != null){
            if(cur1.val < cur2.val){
                cur.next = cur1;
                cur1 = cur1.next;
            }else{
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        cur.next = cur1 == null ? cur2 : cur1;
        return Head.next;
    }
}

Sort a linked list using insertion sort.使用插入排序对链表排序

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
import java.util.*;
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ArrayList<ListNode> list = new ArrayList<ListNode>();
        while(head != null){
            list.add(head);
            head = head.next;
        }
        for(int i = 1;i < list.size();i++){
            for(int j = i - 1;j >= 0 && list.get(j).val > list.get(j+1).val;j--){
                int temp = list.get(j).val;
                list.get(j).val = list.get(j+1).val;
                list.get(j+1).val = temp;
            }
        }
        return list.get(0);
    }
}

Given a binary tree, return the postorder traversal of its nodes' values.后序的

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.*;
public class Solution {
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(root == null){
            return list;
        }
        help(root , list);
        return list;
    }
    public void help(TreeNode root , ArrayList<Integer> list){
        if(root.left != null){
            help(root.left , list);
        }
        if(root.right != null){
            help(root.right , list);
        }
        list.add(root.val);
    }
}

 

全部评论

相关推荐

牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
沉淀一会:1.同学你面试评价不错,概率很大,请耐心等待; 2.你的排名比较靠前,不要担心,耐心等待; 3.问题不大,正在审批,不要着急签其他公司,等等我们! 4.预计9月中下旬,安心过节; 5.下周会有结果,请耐心等待下; 6.可能国庆节前后,一有结果我马上通知你; 7.预计10月中旬,再坚持一下; 8.正在走流程,就这两天了; 9.同学,结果我也不知道,你如果查到了也告诉我一声; 10.同学你出线不明朗,建议签其他公司保底! 11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务