akhilsikri
BAN USERhow about using another node pointer temp2(head->random) along with temp1 (head->next) which is used to reverse the linked list.
Traverse through the linked list
tail=head;
while(head->next != NULL){
temp1 = head->next;
temp2 = head->random;
if (temp2) { // if it's NULL, no need to reverse
temp2->random = head;
}
temp1->next = head;
head = temp1;
}
tail->next = NULL;
how about a state machine with 3 states 3x, 3x+1, 3x+2 where x is just a symbol.
3x denotes divisible by 3 and no remainder
3x+1 denotes remainder is 1
3x+2 denotes remainder is 2
3x moves to 3x+1 if 1 comes and stays in 3x if 0 comes in the stream
3x+1 moves to 3x if 1 comes and moves to 3x+2 if 0 comes in the stream
3x+2 moves to 3x+1 if 0 comes and stays in 3x+2 if 1 comes
Initial states can be
3x if 0 comes (first in the stream)
3x+1 if 1 comes (first in the stream)
This way we will need minimum computation and works with the stream. Only condition is that initial stages should not mess up.
if (top->left != null) {
- akhilsikri December 18, 2011t.Push(top->left);
}
if (top->right != null) {
t.Push(top->right);
These should be other way round. First push right subtree tree and then left to get pre-order in form PLR.