Skip to content

Instantly share code, notes, and snippets.

"""
============================================================================
Question : 2.4 Write code to partition a linked list around a value x,
such that all nodes less than x come before all nodes greater than or equal to x.
Solution : I have three linkedlist, one for less, one for equal, one for greater, get all value from original linkedlist,
and save it to correspoding linkedlist, lastly connect these three linkedlist.
Time Complexity : O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/719d2ca521e7695311c9
============================================================================
"""
============================================================================
Question : 2.5
You have two numbers represented by a linked list, where each node contains a single digit.
The digits are stored in reverse order, such that the Ts digit is at the head of the list.
Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input:(7-> 1 -> 6) + (5 -> 9 -> 2).Thatis,617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
"""
============================================================================
Question : 1.8 Assume you have a method isSubstring which checks if one word is a substring of another.
Given two strings, s1 and s2, write code to check if s2 is
a rotation of s1 using only one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").
Solution : Concatenate s1 with s1, check if s2 is a substring of s1 + s1
Time Complexity : Depend on isSubstring()
Space Complexity: O(n)
Gist Link : https://gist.github.com/habina/33b581c69f59fccfc1a6
============================================================================
"""
============================================================================
Question : 2.6
Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input:A ->B->C->D->E-> C[thesameCasearlier]
Output:C
Solution : Faster pointer moves two steps at a time,
@habina
habina / CC2.7.py
Last active August 29, 2015 14:03
"""
============================================================================
Question : 2.7 Implement a function to check if a linked list is a palindrome.
Solution : Save all elements in to a python list, and use slicing to check if
it is a palindrome
Time Complexity : O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/20fd04778919563f0126
============================================================================
"""
"""
============================================================================
Question : 3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and
Ndisks of different sizes which can slide onto any tower.
The puzzle starts with disks sorted in ascending order of size from top to bottom
(i.e., each disk sits on top of an even larger one).
You have the following constraints:
(1) Only one disk can be moved at a time.
(2) A disk is slid off the top of one tower onto the next tower.
#!/bin/bash
# Allow toggling components to install and update based off flags
updateconsul=1
updatedocker=1
updatedockermachine=1
updatedockercompose=1
updatedockerswarm=1
consulversion="0.6.4"