Skip to content

Instantly share code, notes, and snippets.

@dawand
Last active February 2, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dawand/f5c6b4a6285faac7644fa79577602367 to your computer and use it in GitHub Desktop.
Save dawand/f5c6b4a6285faac7644fa79577602367 to your computer and use it in GitHub Desktop.
Happy Palindrome Day 02022020
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Stack;
public class Palindrome {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
String formattedDate = today.format(DateTimeFormatter.ofPattern("ddMMyyyy"));
System.out.println(formattedDate);
Palindrome p = new Palindrome();
p.checkPalindrome(formattedDate);
}
private void checkPalindrome(String formattedDate) {
ListNode<Character> list = null;
ListNode<Character> head = null;
for (Character c : formattedDate.toCharArray()) {
if (list == null) {
list = new ListNode<>(c);
head = list;
} else {
list.next = new ListNode<>(c);
list = list.next;
}
}
System.out.println(isListPalindrome(head) ? "Happy Palindrome Day" : "not a palindrome day");
}
boolean isListPalindrome(ListNode<Character> l) {
Stack<Character> s = new Stack<>();
ListNode<Character> head = l;
while (head != null) {
s.push(head.value);
head = head.next;
}
head = l;
while (!s.empty() && head != null) {
if (!s.pop().equals(head.value)) {
return false;
}
head = head.next;
}
return true;
}
// Singly-linked list
class ListNode<T> {
ListNode(T x) {
value = x;
}
T value;
ListNode<T> next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment