-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_06.java
70 lines (65 loc) · 1.77 KB
/
Problem_06.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Cracking-The-Coding-Interview
* Problem_06.java
*/
package com.deepak.ctci.Ch02_LinkedLists;
import com.deepak.ctci.Library.LinkedListNode;
/**
* <br> Problem Statement :
*
* Implement a function to check if a linked
* list is a palindrome.
*
* </br>
*
* @author Deepak
*/
public class Problem_06 {
/**
* Method to check if a given linked list is a palindrome
*
* @param head
* @return {@link boolean}
*/
public static <T> boolean isPalindrome(LinkedListNode<T> head) {
/* If head is null, stop processing */
if (head == null) {
return true;
}
/* Reverse the linked list so that we have access to both
* head and reverse head. Then we will compare each item in
* linked list one by one*/
LinkedListNode<T> reverseHead = reverseAndClone(head);
/* Keep checking the elements until both are null */
while (head != null && reverseHead != null) {
/* If at any point, data doesn't match, return false */
if (!head.data.equals(reverseHead.data)) {
return false;
}
/* Increment the pointers */
head = head.next;
reverseHead = reverseHead.next;
}
/* Since we have traversed through the entire list, both
* head and reverseHead should be null */
return head == null && reverseHead == null;
}
/**
* Method to reverse and clone a linked list when only head node is given
*
* @param node
* @return {@link LinkedListNode}
*/
private static <T> LinkedListNode<T> reverseAndClone(LinkedListNode<T> node) {
LinkedListNode<T> head = null;
/* Keep incrementing the node until it is null */
while (node != null) {
/* Clone the node and place it on head */
LinkedListNode<T> newNode = new LinkedListNode<T>(node.data);
newNode.next = head;
head = newNode;
node = node.next;
}
return head;
}
}