Skip to content

Instantly share code, notes, and snippets.

@k1xme
Last active August 29, 2015 14:02
Show Gist options
  • Save k1xme/e63557112ca2253e3d60 to your computer and use it in GitHub Desktop.
Save k1xme/e63557112ca2253e3d60 to your computer and use it in GitHub Desktop.
/*
* 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--;
}
}
@larry-liu
Copy link

Time complexity should be O(n) because your while block loops n/2 times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment