Different CSS Grid frameworks
Twitter Bootstrap960
Foundation
Blueprint
Skeleton
public static Node construct(String preorder, String inorder, int start, int end, int [] preorderIndex)
{
if(start > end)
return null;
Node root = new Node(preorder.charAt(preorderIndex[0]));
++preorderIndex[0];
int inorderIndex = inorder.indexOf(""+root.data);
root.left = construct(preorder, inorder, start, inorderIndex - 1, preorderIndex);
root.right = construct(preorder, inorder, inorderIndex + 1, end, preorderIndex);
return root;
}
public static void main(String[] args) {
String preorder = "ABCDEFG";
String inorder = "GFEDCBA";
// We use this as a reference object, so that we pick the correct value from preorder array during
// recursion. This could be done by having a static variable as well.
int [] preorderIndex = new int[1];
Node root = CodeSamples.construct(preorder, inorder, 0, preorder.length() - 1, preorderIndex);
// Verify if it build correctly
BinaryTree.preorderTraversal(root);
System.out.println();
BinaryTree.inorderTraversal(root);
}
public static Node addLinkedLists(Node head1, Node head2)
{
if(head1 == null)
return head2 != null ? head2 : null;
if(head2 == null)
return head1 != null ? head1 : null;
Stack<node> s1 = new Stack<node>();
Stack<node> s2 = new Stack<node>();
Stack<node> s3 = new Stack<node>();
Node list1 = head1;
Node list2 = head2;
while(list1 != null)
{
s1.push(list1);
list1 = list1.next;
}
while(list2 != null)
{
s2.push(list2);
list2 = list2.next;
}
int carry = 0;
while(!s1.empty() && !s2.empty())
{
int total = carry + s1.pop().data + s2.pop().data;
Node n = new Node(total%10);
s3.push(n);
carry = total/10;
}
while(!s1.empty())
{
int total = carry + s1.pop().data;
Node n = new Node(total%10);
s3.push(n);
carry = total/10;
}
while(!s2.empty())
{
int total = carry + s2.pop().data;
Node n = new Node(total%10);
s3.push(n);
carry = total/10;
}
Node result = s3.pop();
Node retHead = result;
while(!s3.empty())
{
Node n = s3.pop();
result.next = n;
result = n;
}
return retHead;
}
class Singleton
{
private static Singleton _instance;
private Singleton(){ }
public Singleton getInstance()
{
if(_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
public static Node deepCopy(Node head)
{
if(head == null)
return null;
Node oldHead = head;
// Traverse the original doubly linked list and create a new
// singly linked list
// When creating the new linked list save the association between
// corresponding old and new nodes in a hashtable
// For e.g. original: 1->2->3->4
// new: 1'->2'->3'->4'
// hashtable contains mapping of [1->1'], [2->2'],..
Hashtable<node, node> oldToNewNodeMap = new Hashtable<node, node>();
// Create the new head
Node newHead = new Node(oldHead.data+"'");
oldToNewNodeMap.put(oldHead, newHead);
Node tmp = newHead;
oldHead = oldHead.next;
// Create rest of the singly linked list with next pointers
while(oldHead != null)
{
Node newNode = new Node(oldHead.data+"'");
oldToNewNodeMap.put(oldHead, newNode);
tmp.next = newNode;
oldHead = oldHead.next;
tmp = tmp.next;
}
tmp = newHead;
oldHead = head;
// Now assign the prev pointers
while(oldHead != null)
{
tmp.prev = oldToNewNodeMap.get(oldHead.prev);
oldHead = oldHead.next;
tmp = tmp.next;
}
return newHead;
}
public static Node deepCopy(Node head)
{
if(head == null)
return null;
// Rather than using a hastable create new association in
// the following way:
// Create new nodes and insert it into the original linked list
// as follows
// For e.g. 1->1'->2->2'->3->3'->4->4' where 1', 2',.. are
// the new nodes created
Node curNode = head;
while(curNode != null)
{
Node newNode = new Node(curNode.data+"'");
// New node's next points to current node's next
// New node's previous continues to point to current node's prev
newNode.next = curNode.next;
newNode.prev = curNode.prev;
// Current node should now point to the new node
Node next = curNode.next;
curNode.next = newNode;
// Advance to the original list's node
curNode = next;
}
// Now assign correct previous pointer for alternate node (alternate
// nodes are the new nodes but they currently point to the prev
// of original nodes)
curNode = head;
boolean odd = true;
while(curNode != null)
{
if(!odd)
{
curNode.prev = curNode.prev.next;
}
odd = !odd;
curNode = curNode.next;
}
// Now break the linked list into original and new linked list
curNode = head;
Node newHead = curNode.next;
while(curNode != null && curNode.next != null)
{
Node p = curNode;
Node n = curNode.next.next;
curNode = curNode.next;
p.next = n;
}
return newHead;
}
public static Node swapNodes(Node head)
{
Node c = head;
Node p = c;
boolean headSet = false;
while(c != null && c.next != null)
{
Node n = c.next;
c.next = n.next;
n.next = c;
if(!headSet)
{
head = n;
headSet = true;
}
else
{
p.next = n;
p = c;
}
c = c.next;
}
return head;
}
public static Node swapNodes(Node head)
{
Node c = head;
// Create a dummy node before the head node
Node dummy = new Node();
dummy.next = head;
Node p = dummy;
while(c != null && c.next != null)
{
Node n = c.next;
c.next = n.next;
n.next = c;
p.next = n;
p = c;
c = c.next;
}
return dummy.next;
}