Leetcode 341: Flatten Nested List Iterator

341. Flatten Nested List Iterator Total Accepted: 20339 Total Submissions: 55207 Difficulty: Medium Contributors: Admin Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list — whose elements may also be integers or other lists. Example 1:Given the list [[1,1],2,[1,1]], By calling next repeatedly …

Leetcode 27: Remove Element

27. Remove Element  Total Accepted: 154492 Total Submissions: 426032 Difficulty: Easy Contributors: Admin Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be …

Leetcode 29: Divide Two Integers

29. Divide Two Integers  Total Accepted: 82624 Total Submissions: 519755 Difficulty: Medium Contributors: Admin Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.     Code (Naive) import sys class Solution(object): def divide(self, dividend, divisor): “”” :type dividend: int :type divisor: int :rtype: int “”” if not divisor: …

Leetcode 21: Merge Two Sorted Lists

21. Merge Two Sorted Lists Total Accepted: 167826 Total Submissions: 448665 Difficulty: Easy Contributors: Admin Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Code # Definition for singly-linked list. # class ListNode(object): # def __init__(self, …

Leetcode 14: Longest Common Prefix

14. Longest Common Prefix Total Accepted: 131998 Total Submissions: 438812 Difficulty: Easy Contributors: Admin Write a function to find the longest common prefix string amongst an array of strings.   Code class Solution(object): def longestCommonPrefix(self, strs): “”” :type strs: List[str] :rtype: str “”” if not strs: return “” prefix = “” for k in xrange(len(strs[0])): …

Leetcode 24: Swap Nodes in Pairs

24. Swap Nodes in Pairs  Total Accepted: 130373 Total Submissions: 354429 Difficulty: Easy Contributors: Admin Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only …

Leetcode 19: Remove Nth Node From End of List

19. Remove Nth Node From End of List  Total Accepted: 140954 Total Submissions: 445848 Difficulty: Easy Contributors: Admin Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked …

Leetcode 200: Number of Islands

200. Number of Islands Total Accepted: 71131 Total Submissions: 228449 Difficulty: Medium Contributors: Admin Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid …