Last active
February 2, 2020 14:29
-
-
Save dawand/f5c6b4a6285faac7644fa79577602367 to your computer and use it in GitHub Desktop.
Happy Palindrome Day 02022020
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
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