To archive this, I use two pointers, left_tail and right_tail to record both tail of left list and right list. With a variable left_head, I record the pointer to the node whose value equals to head's. 1ListNode *binarySort(ListNode *head, ListNode * left, ListNode *right) {2//zer...
LeetCode之 insertion-sort-list && insertion-sort-list 尝试着刷刷一些经典LeetCode题练练手感,随手做了两道看起来不起眼但还是需要一些细节的题: 1. 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to ...
leetcode 148. Sort List 链表归并排序 Sort a linked list in O(n log n) time using constant space complexity. 本题就是考察的是链表的归并排序。 代码如下: /*class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }*/ public class Solution { public ListNode sortList(Lis...
注意:要考虑空链表,单个元素的链表,以及多个元素的链表。 LeetCode的代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNod...
题目链接 链表的插入排序Sort a linked list using insertion sort.建议:为了操作方便,添加一个额外的头结点。代码如下: 本文地址 1 /** 2 * Def...
find mid of list ListNode fast = head, slow = head, partHead = slow; while (fast != null && fast.next != null) { partHead = slow; fast = fast.next.next; slow = slow.next; } // 2. cut list as binary partHead.next = null; // 3. recursion cut ListNode left = sortList(...
Code Issues Pull requests Discussions Ruby implementation of Algorithms,Data-structures and programming challenges algorithmstackleetcodequicksorthackerrankbubble-sortinsertion-sortleetcode-solutionsbinary-searchcodilitymerge-sortpythagorean-tripleskadanes-algorithmarray-rotationknuth-shuffling-algorithmdutch-nationalflag...
Complete Tree: a binary tree in which every levelexcept possibly the lastis full and all nodes in the last level are as far left as possible A binary search tree, sometimes called BST, is a type of binary tree which maintains the property that the value in each node must be greater tha...
package leetcode /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func insertionSortList(head *ListNode) *ListNode { if head == nil { return head } newHead := &ListNode{Val: 0, Next: nil} // 这里初始化不要直接指向 head,为了...
1.Heap表示方法 满足以下性质的二叉树Binary Tree可以成为Binary Heap: Complete Tree:所有的层都是完全的,除了最后一层,且最后一层的叶子靠左。 Min Heap or Max Heap:根节点是最大值或者最小值,而且这个性质对于任何递归得到的子树都成立。 Binary Heap通常使用array表示: 根节点在array[0]; array[(i-1)//...