Skip to content

Instantly share code, notes, and snippets.

@xun-gong
xun-gong / CareerCup2.3.cpp
Created July 1, 2014 15:21
CareerCup2.3.cpp
/**
* Chapter 2
* 2.3 Implement an algotithm to delete a node in the middle of a singly linked list.
* Given only get access to that node.
* EXAMPLE
* Input: the node c from the linked list a->b->c->d->e
* Result: nothing is returned, but the new linked list looks like a->b->d->e
*/
#include <iostream>
@xun-gong
xun-gong / CareerCup2.4.cpp
Created July 1, 2014 15:26
CareerCup2.4.cpp
/**
* Chapter 2
*
* 2.4 Write a 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
*/
#include <iostream>
using namespace std;
@xun-gong
xun-gong / CareerCup2.5.cpp
Created July 4, 2014 03:51
CareerCup2.5.cpp
/**
* Chapter 2
*
* 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 1's digit
* is at the head of the list. Write a function that adds the two numbers and returns
* the sum as a linked list.
* FOLLOW UP:
* Suppose the digits are stored in forward order. Repeat the above problem.
*/
@xun-gong
xun-gong / CareerCup2.6.cpp
Created July 4, 2014 03:57
CareerCup2.6.cpp
/**
* Chapter 2
*
* 2.6 Given a circular linked list, implement an algorithm which returns
* the node at the beginning of the loop.
* Input: A -> B -> C -> D -> E -> C (The same C as before, not a new node)
* Output: C
*/
#include <iostream>
@xun-gong
xun-gong / CareerCup2.7.cpp
Created July 6, 2014 21:51
CareerCup2.7.cpp
/**
* Chapter 2
*
* 2.7 Implement a function to check if a linked list is a palindrome.
*
*/
#include <iostream>
#include <stack>
using namespace std;
@xun-gong
xun-gong / CareerCup3.1.cpp
Created July 6, 2014 21:58
CareerCup3.1.cpp
/*
* Chapter 3
*
* 3.1 Describe hwo you could use a single array to implement three stacks.
*
*/
#include <iostream>
using namespace std;
@xun-gong
xun-gong / CareerCup3.2.cpp
Created July 6, 2014 22:04
CareerCup3.2.cpp
/*
* Chapter 3
*
* 3.2 How could you design a stack which, in addition to push and pop, also
* has a function min which returns the minimum element? Push, pop and min
* should all operation in O(1) time.
*
*/
@xun-gong
xun-gong / CareerCup3.3.cpp
Created July 6, 2014 22:06
CareerCup3.3.cpp
/*
* Chapter 3
*
* 3.3 Implement a data structure SetOfStacks.
* SetOfStacks should be composed of several stacks and should
* creat new stack once that previous one exeeds capacity.
* SetOfStack.push() and SetOfStack.pop() should behave identically to a single stack.
* FOLLOW UP
* Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.
*/
@xun-gong
xun-gong / CareerCup3.6.cpp
Created July 7, 2014 18:55
CareerCup3.6.cpp
/*
* Chapter 3
*
* 3.6 Write a program to sort a stack in ascending order(with biggest on top)
* You may use at most one additional stack to hold items, but you may not
* copy the elements into any other data structure. The stack supports the
* following operation: push, pop, peek, isEmpty
*/
/* This sort algorithm supports duplicated elements */
@xun-gong
xun-gong / CareerCup3.5.cpp
Created July 7, 2014 19:08
CareerCup3.5.cpp
/*
* Chapter 3
*
* 3.5 Implement a MyQueue class with implements a queue
* using two stacks.
*
*/
#include <iostream>
#include <stack>