Last active
August 29, 2015 14:02
-
-
Save k1xme/e63557112ca2253e3d60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Space complexity: O(1); Time complexity: O(n). | |
*/ | |
void reverseString(char* str){ | |
if(str == NULL) return; // Validation checking. | |
if(*str == "\0") return; // Empty string. | |
char temp; // For swapping tail and head. | |
char* head=str, tail=str; // Pointers for the beginning and the end of the string. | |
while(*tail != "\0") tail++; // Find the end of the string. | |
tail--; | |
while(head<=tail){ // Swap the string. | |
temp = *head; | |
*head = *tail; | |
*tail = temp; | |
head++; | |
tail--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time complexity should be O(n) because your while block loops n/2 times.