-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoublyLinkedListPalyndrome.java
More file actions
39 lines (35 loc) · 1.14 KB
/
Copy pathDoublyLinkedListPalyndrome.java
File metadata and controls
39 lines (35 loc) · 1.14 KB
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
public class DoublyLinkedListPalyndrome {
public <T> boolean isPalyndrome(Node<T> head, Node<T> tail) {
if (head == null || tail == null)
return false;
while (head != tail || head != null && head.prev != tail) {
if (head.data != tail.data)
return false;
head = head.next;
tail = tail.prev;
}
return true;
}
public static void main(String[] args) {
DoublyLinkedListPalyndrome dllp = new DoublyLinkedListPalyndrome();
//String str = "abcdefghgfedcba";
String str = "abcba";
Node<Character> head = new Node<>();
head.data = str.charAt(0);
Node<Character> current = head;
for (int i = 1; i < str.length(); i++) {
Node<Character> newNode = new Node<>();
newNode.data = str.charAt(i);
current.next = newNode;
newNode.prev = current;
current = current.next;
}
Node<Character> tail = current;
System.out.println(dllp.isPalyndrome(head, tail));
}
}
class Node<T> {
T data;
Node<T> next;
Node<T> prev;
}