Skip to content

Commit 544554a

Browse files
committed
Palindromic Linked List
1 parent e1c9d93 commit 544554a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Java/src/CodeRunner/LinkedListsRunner.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public static void main(String[] args) {
1616

1717
// System.out.println(testLinkedListReversalRecursive());
1818

19+
System.out.println(testPalindromicLinkedList());
20+
1921
// System.out.println(testLRUCache());
2022

2123
}
@@ -133,6 +135,27 @@ private static boolean testLinkedListReversalRecursive() {
133135
return true;
134136
}
135137

138+
private static boolean testPalindromicLinkedList() {
139+
int[] valsA = {1, 2, 3, 4, 5, 4, 3, 2, 1};
140+
int[] valsB = {1, 2, 3, 4, 4, 3, 2, 1};
141+
142+
ListNode headA = makeList(valsA);
143+
ListNode headB = makeList(valsB);
144+
145+
System.out.println("Printing list A");
146+
System.out.println(listNodeToString(headA));
147+
148+
System.out.println("Printing list B");
149+
System.out.println(listNodeToString(headB));
150+
151+
if (!(PalindromicLinkedList.Solution(headA) && PalindromicLinkedList.Solution(headB))) {
152+
return false;
153+
}
154+
155+
System.out.println("Both lists are palindrome");
156+
return true;
157+
}
158+
136159
private static boolean testLRUCache() {
137160
LRUCache lru1 = new LRUCache(3);
138161

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package LinkedLists;
2+
3+
import DS.ListNode;
4+
5+
/*
6+
// Definition of ListNode:
7+
public class ListNode {
8+
public int val;
9+
public ListNode next;
10+
public ListNode(int val) { this.val = val; }
11+
public ListNode(int val, ListNode next) {
12+
this(val);
13+
this.next = next;
14+
}
15+
}
16+
*/
17+
18+
19+
public class PalindromicLinkedList {
20+
public static boolean Solution(ListNode head) {
21+
// Find the middle of the linked list and then reverse the second half of the
22+
// linked list starting at this midpoint.
23+
ListNode mid = findMiddle(head);
24+
ListNode secondHead = reverseList(mid);
25+
// Compare the first half and the reversed second half of the list
26+
ListNode ptr1 = head;
27+
ListNode ptr2 = secondHead;
28+
boolean result = true;
29+
while (ptr2 != null) {
30+
if (ptr1.val != ptr2.val) {
31+
result = false;
32+
}
33+
ptr1 = ptr1.next;
34+
ptr2 = ptr2.next;
35+
}
36+
return result;
37+
}
38+
39+
// From the 'Reverse Linked List' problem.
40+
private static ListNode reverseList(ListNode head) {
41+
ListNode currNode = head;
42+
ListNode prevNode = null;
43+
while (currNode != null) {
44+
ListNode nextNode = currNode.next;
45+
currNode.next = prevNode;
46+
prevNode = currNode;
47+
currNode = nextNode;
48+
}
49+
return prevNode;
50+
}
51+
52+
// From the 'Linked List Midpoint' problem.
53+
private static ListNode findMiddle(ListNode head) {
54+
ListNode p1 = head;
55+
ListNode p2 = head;
56+
while (p2 != null && p2.next != null) {
57+
p1 = p1.next;
58+
p2 = p2.next.next;
59+
}
60+
return p1;
61+
}
62+
}

0 commit comments

Comments
 (0)