Skip to content

Instantly share code, notes, and snippets.

@ademidun
Created February 12, 2016 22:59
Show Gist options
  • Save ademidun/1372726af1615a8057e1 to your computer and use it in GitHub Desktop.
Save ademidun/1372726af1615a8057e1 to your computer and use it in GitHub Desktop.
Node* Reverse(Node *head)
{
Node* trail=0;
while (head){
Node* next= head->next;
head->next = trail;
trail=head;
head=next;
}
return trail;
}
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