Posts

Showing posts from November, 2013

Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in  Reverse Polish Notation . Valid operators are  + ,  - ,  * ,  / . Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 Bug free. Could done better if we add more error handling (instead of checking +-*/, we could have more invalid inputs)

Insertion Sort List

Sort a linked list using insertion sort. Bug free. Probably could do better.

Binary Tree Preorder Traversal

Given a binary tree, return the  preorder  traversal of its nodes' values. For example: Given binary tree  {1,#,2,3} , 1 \ 2 / 3 return  [1,2,3] . Recursive: Iterative:

Reorder List

Given a singly linked list  L :  L 0 → L 1 →…→ L n -1 → L n , reorder it to:  L 0 → L n → L 1 → L n -1 → L 2 → L n -2 →… You must do this in-place without altering the nodes' values. For example, Given  {1,2,3,4} , reorder it to  {1,4,2,3} . Here I use a double queue to achieve O(n) complexity.