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);
    }
}

 

全部评论

相关推荐

02-25 16:55
已编辑
北京工业大学 Java
211本,找日常实习的话,如果面向中厂的话,需要刷hot100么?因为之前从来没刷过,算法仅限于学校课程水平,准备3月投递简历,现在还需要背八股文,时间有些紧张,还需要刷算法题么?同时什么样的公司可以算是中厂呢?
程序员小白条:中大厂说的上名字的,必定要算法,hot100只是最基础的了,题库远不止100题捏,一般在300-400题量之间,算法=学校课程=简单题也做不出,多准备八股文和算法吧,其他项目可以放放,精刷算法就行了,花时间成长很快的
点赞 评论 收藏
分享
开发转测第二人:没实习的话,两个项目吧,八股也要准备一下,这个时间点有点小晚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务