Created
February 12, 2016 22:59
-
-
Save ademidun/1372726af1615a8057e1 to your computer and use it in GitHub Desktop.
This file contains 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
Node* Reverse(Node *head) | |
{ | |
Node* trail=0; | |
while (head){ | |
Node* next= head->next; | |
head->next = trail; | |
trail=head; | |
head=next; | |
} | |
return trail; | |
} |
This file contains 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
bool isValid(string s) { | |
stack<char> st; | |
for (int i = 0; i < s.length(); i++) { | |
if (s[i] == '(' || s[i] == '[' || s[i] == '{') | |
st.push(s[i]); | |
else { | |
if (!st.empty()) { | |
switch (st.top()) | |
{ | |
case '(': | |
if (s[i] == ')') st.pop(); | |
else return false; | |
break; | |
case '{': | |
if (s[i] == '}') st.pop(); | |
else return false; | |
break; | |
case '[': | |
if (s[i] == ']') st.pop(); | |
else return false; | |
break; | |
} | |
} | |
else return false; | |
} | |
} | |
return st.empty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment