Created
January 29, 2016 00:13
-
-
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
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) { | |
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; | |
} |
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
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