two-pointers:

Remove Duplicates From Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1 Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example 2 Input: 1->1->1->2->3 Output: 2->3

by lek tin in "algorithm" access_time 1-min read

Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 Solution # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ pre = ListNode(0) pre.next = head pos = pre while pos.next != None: if pos.

by lek tin in "algorithm" access_time 1-min read

Remove Element

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn’t matter what you leave beyond the new length. Example 1 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2.

by lek tin in "algorithm" access_time 2-min read

80. Remove Duplicates From Sorted Array II

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1 Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.

by lek tin in "algorithm" access_time 2-min read

Remove Duplicates From Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1 Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length.

by lek tin in "algorithm" access_time 2-min read

3sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution # time: o(n^2) # space: o(1) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: results = [] if not nums: return results nums.

by lek tin in "algorithm" access_time 2-min read

Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head. Example Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note Given n will always be valid. Follow-up Could you do this in one pass? Solution # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.

by lek tin in "algorithm" access_time 1-min read