patternjavaMinor
Check for palindrome in singly linked list
Viewed 0 times
palindromesinglyforlistchecklinked
Problem
Looking for code review, optimizations and best practices.
Node.java
Palindrome.java
```
public class Palindrome {
static Node reverse(Node head) {
Node prev = null;
Node current = head;
Node next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
static public boolean isPalindrome(Node head) {
Node current = head;
int length = 0;
// Get the length of the LinkedList
while (current != null) {
current = current.next;
length++;
}
// Find position of next to mid element
int mid = length % 2 == 0 ? length / 2 + 1 : length / 2 + 2;
int idx = mid;
current = head;
// Traverse to next to mid element
while (idx > 1) {
current = current.next;
idx--;
}
// Reverse right side of the List and return head of the reversed list
Node head1 = reverse(current);
boolean isPalindrome = true;
idx = length / 2;
current = head;
// Check reversed list(second half) matches with first half
while (idx > 0) {
if (current.data != head1.data) {
isPalindrome = false;
break;
}
current = current.next;
head1 = head1.next;
idx--;
}
return isPalindrome;
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.nex
Node.java
class Node {
Node next;
int data;
public Node(int value) {
data = value;
}
}Palindrome.java
```
public class Palindrome {
static Node reverse(Node head) {
Node prev = null;
Node current = head;
Node next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
static public boolean isPalindrome(Node head) {
Node current = head;
int length = 0;
// Get the length of the LinkedList
while (current != null) {
current = current.next;
length++;
}
// Find position of next to mid element
int mid = length % 2 == 0 ? length / 2 + 1 : length / 2 + 2;
int idx = mid;
current = head;
// Traverse to next to mid element
while (idx > 1) {
current = current.next;
idx--;
}
// Reverse right side of the List and return head of the reversed list
Node head1 = reverse(current);
boolean isPalindrome = true;
idx = length / 2;
current = head;
// Check reversed list(second half) matches with first half
while (idx > 0) {
if (current.data != head1.data) {
isPalindrome = false;
break;
}
current = current.next;
head1 = head1.next;
idx--;
}
return isPalindrome;
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.nex
Solution
Your inconsistent and unconventional indentation makes your code harder to read than it should be.
Although Java accepts multiple top-level classes in the same file as long as no more than one is public, it is poor form. Every top-level class, interface, and enum should have its own file, whether public or not. Among other things, this makes it much easier to find all the classes, both for you and for the compiler.
It is a bit questionable that you're willing to destroy the list in order to test whether it's palindromic, but inasmuch as you are willing to do so, you could perform your palindrome test more efficiently. You would use two primary Node references, one that advances one position for every two that the other advances. Using the trailing reference, you would reverse the front portion of the list as you go, so that when the lead reference finds the end of the list, the trailing reference is at the midpoint, with a reference to the reversed first half already in hand. I'm sure you see how to proceed from there.
You could also consider using an auxiliary data structure to build the reverse half-list or an equivalent; in particular, pushing the nodes one by one onto a stack would have a comparable effect. This could be done in a way that does not destroy the original list.
Or on the third hand, if you were willing to change to a doubly-linked list, and especially if you were willing to represent the list itself with a separate data structure that contained a reference to the tail in addition to the reference to the head, then the palindrome test could be performed much more simply. You would just start at both ends and work towards the middle. That doesn't modify the list, either.
Although Java accepts multiple top-level classes in the same file as long as no more than one is public, it is poor form. Every top-level class, interface, and enum should have its own file, whether public or not. Among other things, this makes it much easier to find all the classes, both for you and for the compiler.
It is a bit questionable that you're willing to destroy the list in order to test whether it's palindromic, but inasmuch as you are willing to do so, you could perform your palindrome test more efficiently. You would use two primary Node references, one that advances one position for every two that the other advances. Using the trailing reference, you would reverse the front portion of the list as you go, so that when the lead reference finds the end of the list, the trailing reference is at the midpoint, with a reference to the reversed first half already in hand. I'm sure you see how to proceed from there.
You could also consider using an auxiliary data structure to build the reverse half-list or an equivalent; in particular, pushing the nodes one by one onto a stack would have a comparable effect. This could be done in a way that does not destroy the original list.
Or on the third hand, if you were willing to change to a doubly-linked list, and especially if you were willing to represent the list itself with a separate data structure that contained a reference to the tail in addition to the reference to the head, then the palindrome test could be performed much more simply. You would just start at both ends and work towards the middle. That doesn't modify the list, either.
Context
StackExchange Code Review Q#125605, answer score: 3
Revisions (0)
No revisions yet.