Professional Documents
Culture Documents
C/C++ Interview Algorithms: 1. Reverse A Singly Linked List
C/C++ Interview Algorithms: 1. Reverse A Singly Linked List
Page 1 of 8
4. Reverse a string
void ReverseString (char *String) { char *Begin = String; char *End = String + strlen(String) - 1; char TempChar = '\0'; while (Begin < End) { TempChar = *Begin; *Begin = *End; *End = TempChar; Begin++; End--; } }
Page 2 of 8
Page 3 of 8
printf("%d",strtoint("0123")); printf("\n%d",strtoint("0123ii"));
Page 4 of 8
10.
// // recursive version // void PrintNum ( int Num ) { if ( Num == 0 ) return; PrintNum ( Num / 10 ); Puthcar ( 0+ Num % 10 ); }
Page 5 of 8
// // iterative version // int Factorial( int Num ) { int I int result = 1; for ( I= Num; I > 0; I-- ) { result = result * I; } } return result;
Page 6 of 8
13. Write a function that finds the last instance of a character in a string
char *lastchar(char *String, char ch) { char *pStr = NULL; // traverse the entire string while( * String ++ != NULL ) { if( *String == ch ) pStr = String; } return pStr; }
14. Return Nth the node from the end of the linked list in one pass.
Page 7 of 8
15.
Page 8 of 8