# For example, # Given 1->4->3->2->5->2->null and x = 3, # return 1->2->2->4->3->5->null. from ListNode import ListNode def partitionList(head, x): # create two list:left and right leftDummy = ListNode(0) leftTail = leftDummy rightDummy = ListNode(0) rightTail = rightDummy while head != None: if head.val < x: leftTail.next = head leftTail = leftTail.next else: rightTail.next = head rightTail = rightTail.next head = head.next # combine two list rightTail.next = None leftTail.next = rightDummy.next return leftDummy.next ListNode.printList(partitionList(ListNode.arrayToList([1, 4, 3, 2, 5, 2]), 3))
# Given a sorted linked list, delete all duplicates such that each element # appear only once. # Have you met this question in a real interview? Yes # Example # Given 1->1->2, return 1->2. # Given 1->1->2->3->3, return 1->2->3. from ListNode import ListNode def removeDuplicatesfromSortedList(head): if head == None: return None node = head while node.next != None: if node.val == node.next.val: node.next = node.next.next else: node = node.next return head # head1 = ListNode.arrayToList([1, 1, 2]) # ListNode.printList(removeDuplicatesfromSortedList(head1)) head2 = ListNode.arrayToList([1, 1, 2, 3, 3]) ListNode.printList(removeDuplicatesfromSortedList(head2))
# Given a singly linked list L: L0 -> L1 -> ... ->Ln-1 -> Ln, # reorder it to: L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ... # You must do this in-place without altering the nodes' values. # For example, # Given 1 -> 2 -> 3 -> 4 -> null, reorder it to 1 -> 4 -> 2 -> 3 -> null. from ListNode import ListNode def reorderList(head): if head == None: return None head = ListNode.arrayToList([1, 4, 2, 3]) ListNode.printList(reorderList(head))
# Remove all elements from a linked list of integers that have value val. # Have you met this question in a real interview? Yes # Example # Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5 from ListNode import ListNode def removeLinkedListElements(head, val): if head == None or val == None: return head dummy = ListNode(0) dummy.next = head head = dummy while head.next != None: if head.next.val == val: head.next = head.next.next else: head = head.next return dummy.next head = ListNode.arrayToList([1, 2, 3, 3, 4, 5, 3]) ListNode.printList(removeLinkedListElements(head, 3))
slow = slow.next fast = fast.next.next return slow # helper: merge two sorted linked list def merge(head1, head2): if head1 == None and head2 == None: return None dummy = ListNode(0) lastNode = dummy while head1 != None and head2 != None: if head1.val < head2.val: lastNode.next = head1 head1 = head1.next else: lastNode.next = head2 head2 = head2.next lastNode = lastNode.next if head1 != None: lastNode.next = head1 else: lastNode.next = head2 return dummy.next head = ListNode.arrayToList([1, 3, 2, 4, 5, 7, 6]) ListNode.printList(sortList(head)) ListNode.printList(sortListI(head))
tail = dummy carry = 0 while l1 is not None and l2 is not None: s = l1.val + l2.val + carry tail.next = ListNode(s % 10) carry = s / 10 l1 = l1.next l2 = l2.next tail = tail.next while l1 is not None: s = l1.val + carry tail.next = ListNode(s % 10) carry = s / 10 l1 = l1.next tail = tail.next while l2 is not None: s = l2.next + carry tail.next = ListNode(s % 10) carry = s / 10 l2 = l2.next tail = tail.next if carry != 0: tail.next = ListNode(carry) return dummy.next l1 = ListNode.arrayToList([6, 1, 7]) l2 = ListNode.arrayToList([2, 9, 5]) ListNode.printList(addTwoNumbersII(l1, l2))
# Find the nth to last element of a singly linked list. # The minimum number of nodes in list is n. # Have you met this question in a real interview? Yes # Example # Given a List 3->2->1->5->null and n = 2, return node whose value is 1. from ListNode import ListNode def nthtoLastNodeinList(head, n): if head is None or n is None: return None dummy = ListNode(0) dummy.next = head fast = head slow = head for i in range(n): fast = fast.next while fast is not None: fast =fast.next slow = slow.next return slow head = ListNode.arrayToList([3, 2, 1, 5]) print nthtoLastNodeinList(head, 2).val
# Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null. from ListNode import ListNode def mergeTwoSortedLists(l1, l2): if l1 == None and l2 == None: return None dummy = ListNode(0) tail = dummy while l1 != None and l2 != None: if l1.val < l2.val: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next tail = tail.next if l1 != None: tail.next = l1 else: tail.next = l2 return dummy.next head1 = ListNode.arrayToList([1, 3, 8, 11, 15]) head2 = ListNode.arrayToList([2]) ListNode.printList(mergeTwoSortedLists(head1, head2))
mid = start + (end - start) / 2 left = mergeKHelper(lists, start, mid) right = mergeKHelper(lists, mid + 1, end) return mergeTwoSortedList(left, right) def mergeTwoSortedList(head1, head2): if head1 == None and head2 == None: return None dummy = ListNode(0) tail = dummy while head1 != None and head2 != None: if head1.val < head2.val: tail.next = head1 head1 = head1.next else: tail.next = head2 head2 = head2.next tail = tail.next if head1 != None: tail.next = head1 else: tail.next = head2 return dummy.next head1 = ListNode.arrayToList([2, 4]) head2 = ListNode.arrayToList([-1]) ListNode.printList(mergeKSortedLists([head1, head2]))
# return 1->2->2->4->3->5->null. from ListNode import ListNode def partitionList(head, x): # create two list:left and right leftDummy = ListNode(0) leftTail = leftDummy rightDummy = ListNode(0) rightTail = rightDummy while head != None: if head.val < x: leftTail.next = head leftTail = leftTail.next else: rightTail.next = head rightTail = rightTail.next head = head.next # combine two list rightTail.next = None leftTail.next = rightDummy.next return leftDummy.next ListNode.printList(partitionList(ListNode.arrayToList([1, 4, 3, 2, 5, 2]), 3))
def insertionSortList(head): if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head prev = head head = head.next while head != None: temp1 = head.next iNode = dummy while iNode.next != head: if iNode.next.val > head.val: temp2 = iNode.next iNode.next = head head.next = temp2 prev.next = temp1 else: iNode = iNode.next prev = head head = temp2 return head print 1 head = ListNode.arrayToList([1, 3, 2, 0]) ListNode.printList(insertionSortList(head))
carry = 0 while l1 is not None and l2 is not None: sum = l1.val + l2.val + carry carry = sum / 10 point.next = ListNode(sum % 10) l1 = l1.next l2 = l2.next point = point.next while l1 is not None: sum = l1.next + carry carry = sum / 10 point.next = ListNode(sum % 10) l1 = l1.next point = point.next while l2 is not None: sum = l2.val + carry carry = sum / 10 point.next = ListNode(sum % 10) l2 = l2.next point = point.next if carry != 0: point.next = ListNode(carry) return dummy.next l1 = ListNode.arrayToList([7, 1, 6]) l2 = ListNode.arrayToList([5, 9, 2]) ListNode.printList(addTwoNumbers(l1, l2))
# Find the middle node of a linked list. # Have you met this question in a real interview? Yes # Example # Given 1->2->3, return the node with value 2. # Given 1->2, return the node with value 1. from ListNode import ListNode def middleofLinkedList(head): if head == None: return head slow = head fast = head.next while fast != None and fast.next != None: slow = slow.next fast = fast.next.next return slow print middleofLinkedList(ListNode.arrayToList([1, 2, 3])).val print middleofLinkedList(ListNode.arrayToList([1, 2])).val
# Find the nth to last element of a singly linked list. # The minimum number of nodes in list is n. # Have you met this question in a real interview? Yes # Example # Given a List 3->2->1->5->null and n = 2, return node whose value is 1. from ListNode import ListNode def nthtoLastNodeinList(head, n): if head is None or n is None: return None dummy = ListNode(0) dummy.next = head fast = head slow = head for i in range(n): fast = fast.next while fast is not None: fast = fast.next slow = slow.next return slow head = ListNode.arrayToList([3, 2, 1, 5]) print nthtoLastNodeinList(head, 2).val
# Implement an algorithm to delete a node in the middle of a singly linked list, # given only access to that node. # Have you met this question in a real interview? Yes # Example # Given 1->2->3->4, and node 3. return 1->2->4 from ListNode import ListNode def deleteNodeintheMiddleofSinglyLinkedList(node): if node is None: return # node is not last node if node.next is not None: node.val = node.next.val node.next = node.next.next # node is the last node else: node = None head = ListNode.arrayToList([1, 2, 3, 4]) node = head.next.next deleteNodeintheMiddleofSinglyLinkedList(node) ListNode.printList(head)
# Example # Given 1->2->3->3->4->4->5, return 1->2->5. # Given 1->1->1->2->3, return 2->3. from ListNode import ListNode def removeDupfromSortedListII(head): if head == None: return None dummy = ListNode(0) dummy.next = head head = dummy while head.next != None and head.next.next != None: if head.next.val == head.next.next.val: val = head.next.val while head.next != None and head.next.val == val: head.next = head.next.next else: head = head.next return dummy.next head1 = ListNode.arrayToList([1, 2, 3, 3, 4, 4, 5]) head2 = ListNode.arrayToList([1, 1, 1, 2, 3]) ListNode.printList(removeDupfromSortedListII(head2))
from ListNode import ListNode def mergeTwoSortedLists(l1, l2): if l1 == None and l2 == None: return None dummy = ListNode(0) tail = dummy while l1 != None and l2 != None: if l1.val < l2.val: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next tail = tail.next if l1 != None: tail.next = l1 else: tail.next = l2 return dummy.next head1 = ListNode.arrayToList([1, 3, 8, 11, 15]) head2 = ListNode.arrayToList([2]) ListNode.printList(mergeTwoSortedLists(head1, head2))
newCapacity = 2 * len(hashTable) newHashTable = [None for i in range(newCapacity)] for i in range(len(hashTable)): while hashTable[i] != None: newIndex = (hashTable[i].val % newCapacity + newCapacity) % newCapacity if newHashTable[newIndex] == None: newHashTable[newIndex] = ListNode(hashTable[i].val) else: dummy = newHashTable[newIndex] while dummy.next != None: dummy = dummy.next dummy.next = ListNode(hashTable[i].val) hashTable[i] = hashTable[i].next return newHashTable head1 = ListNode.arrayToList([21, 9]) head2 = ListNode.arrayToList([14]) hashTable = [None, head1, head2, None] print rehashing(hashTable)
# Have you met this question in a real interview? Yes # Example # Given 1->3->2->1->4. # Return 1->3->2->4 # Challenge # (hard) How would you solve this problem if a temporary buffer is not allowed? # In this case, you don't need to keep the order of nodes. from ListNode import ListNode def removeDuplicatesfromUnsortedList(head): if head == None: return None dummy = ListNode(0) dummy.next = head head = dummy s = set() while head.next != None: if head.next.val in s: head.next = head.next.next else: s.add(head.next.val) head = head.next return dummy.next head = ListNode.arrayToList([1, 3, 2, 1, 4]) ListNode.printList(removeDuplicatesfromUnsortedList(head))
# Python: you can directly use -1 % 3, you will get 2 automatically. from ListNode import ListNode def rehashing(hashTable): if len(hashTable) == 0: return hashTable newCapacity = 2 * len(hashTable) newHashTable = [None for i in range(newCapacity)] for i in range(len(hashTable)): while hashTable[i] != None: newIndex = (hashTable[i].val % newCapacity + newCapacity) % newCapacity if newHashTable[newIndex] == None: newHashTable[newIndex] = ListNode(hashTable[i].val) else: dummy = newHashTable[newIndex] while dummy.next != None: dummy = dummy.next dummy.next = ListNode(hashTable[i].val) hashTable[i] = hashTable[i].next return newHashTable head1 = ListNode.arrayToList([21, 9]) head2 = ListNode.arrayToList([14]) hashTable = [None, head1, head2, None] print rehashing(hashTable)
def removeDupfromSortedListII(head): if head == None: return None dummy = ListNode(0) dummy.next = head head = dummy while head.next != None and head.next.next != None: if head.next.val == head.next.next.val: val = head.next.val while head.next != None and head.next.val == val: head.next = head.next.next else: head = head.next return dummy.next head1 = ListNode.arrayToList([1, 2, 3, 3, 4, 4, 5]) head2 = ListNode.arrayToList([1, 1, 1, 2, 3]) ListNode.printList(removeDupfromSortedListII(head2))