Skip to content

Instantly share code, notes, and snippets.

@mding5692
Created January 29, 2016 00:13
Show Gist options
  • Save mding5692/0911a7843431bed90f5e to your computer and use it in GitHub Desktop.
Save mding5692/0911a7843431bed90f5e to your computer and use it in GitHub Desktop.
For Western Tech Interview Prep Assignment 1 : Reverse a Linked List and Valid Palindrome
Node Reverse(Node head) {
if (head == null || head.next == null) {
return head;
}
Node curr = null;
Node nextNode = null;
while (true) {
nextNode = head.next;
head.next = curr;
curr = head;
if (nextNode == null ) {
break;
}
head = nextNode;
}
return head;
}
public class Solution {
public boolean isPalindrome(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
if (s.length() == 1) {
return true;
}
for (int i=0; i < s.length()/2; i++) {
int j = s.length() - 1 - i;
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment