Leetcode 147: Insertion Sort List

https://leetcode.com/problems/insertion-sort-list/ Sort a linked list using insertion sort.   Code  # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): “”” :type head: ListNode :rtype: ListNode “”” new_head = ListNode(0) cur = head while cur: isrt = new_head next = …

Leetcode 164: Maximum Gap

https://leetcode.com/problems/maximum-gap/ Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.   Code …

Leetcode 71: Simplify Path

https://leetcode.com/problems/simplify-path/ Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../"?In this case, you should return "/". Another corner case is the path might contain multiple slashes '/' together, …

Leetcode 89: Gray Code

https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its …

Leetcode 95: Unique Binary Search Trees II

https://leetcode.com/problems/unique-binary-search-trees-ii/ Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example,Given n = 3, your program should return all 5 unique BST’s shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 …

Leetcode 97: Interleaving String

https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.   Code class Solution(object): def isInterleave(self, s1, s2, s3): “”” :type s1: str :type s2: str :type s3: str :rtype: bool …