Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d861890

Browse files
committed
LinkedList palindrome done
1 parent 32b988e commit d861890

File tree

3 files changed

+99
-31
lines changed

3 files changed

+99
-31
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ctci.linkedlists;
2+
3+
import java.util.Stack;
4+
5+
import static com.ctci.linkedlists.Node.printList;
6+
7+
/**
8+
* @author rampatra
9+
* @since 2019-02-02
10+
*/
11+
public class Palindrome {
12+
13+
/**
14+
* Checks whether a Linked List is palindrome by using a stack.
15+
*
16+
* @param head starting node of the linked list.
17+
* @return {@code true} if linked list palindrome, {@code false} otherwise.
18+
*/
19+
private static boolean isPalindrome(Node head) {
20+
Node curr = head;
21+
Stack<Integer> stack = new Stack<>();
22+
while (curr != null) {
23+
stack.push(curr.val);
24+
curr = curr.next;
25+
}
26+
curr = head;
27+
while (!stack.empty() && curr != null) {
28+
if (stack.pop() != curr.val) {
29+
return false;
30+
}
31+
curr = curr.next;
32+
}
33+
return true;
34+
}
35+
36+
public static void main(String[] args) {
37+
38+
Node l1 = new Node(1);
39+
l1.next = new Node(2);
40+
l1.next.next = new Node(3);
41+
l1.next.next.next = new Node(3);
42+
l1.next.next.next.next = new Node(2);
43+
l1.next.next.next.next.next = new Node(1);
44+
printList(l1);
45+
System.out.println(isPalindrome(l1));
46+
System.out.println("------");
47+
48+
l1 = new Node(1);
49+
l1.next = new Node(2);
50+
l1.next.next = new Node(3);
51+
l1.next.next.next = new Node(2);
52+
l1.next.next.next.next = new Node(1);
53+
printList(l1);
54+
System.out.println(isPalindrome(l1));
55+
System.out.println("------");
56+
57+
l1 = new Node(0);
58+
l1.next = new Node(2);
59+
l1.next.next = new Node(3);
60+
l1.next.next.next = new Node(3);
61+
l1.next.next.next.next = new Node(0);
62+
printList(l1);
63+
System.out.println(isPalindrome(l1));
64+
System.out.println("------");
65+
66+
l1 = new Node(1);
67+
printList(l1);
68+
System.out.println(isPalindrome(l1));
69+
}
70+
}

src/main/java/com/rampatra/linkedlists/IsIdentical.java renamed to src/main/java/com/rampatra/linkedlists/Identical.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
*
99
* @author rampatra
1010
* @since 6/29/15
11-
* @time: 9:42 AM
1211
*/
13-
public class IsIdentical {
12+
public class Identical {
1413

1514
/**
1615
* Returns {@code true} if linked list {@param list1} and {@param list2}
@@ -22,13 +21,13 @@ public class IsIdentical {
2221
* @param <E>
2322
* @return
2423
*/
25-
public static <E extends Comparable<E>> boolean isIdentical(SingleLinkedList<E> list1,
24+
private static <E extends Comparable<E>> boolean isIdentical(SingleLinkedList<E> list1,
2625
SingleLinkedList<E> list2) {
2726

2827
// base cases
2928
if (list1.size != list2.size) {
3029
return false;
31-
} else if (list1.size == 0 && list2.size == 0) {
30+
} else if (list1.size == 0) {
3231
return true;
3332
}
3433

@@ -45,24 +44,24 @@ public static <E extends Comparable<E>> boolean isIdentical(SingleLinkedList<E>
4544
return true;
4645
}
4746

48-
public static void main(String a[]) {
47+
public static void main(String[] a) {
4948
SingleLinkedList<Integer> linkedList1 = new SingleLinkedList<>();
50-
linkedList1.add(00);
51-
linkedList1.add(11);
52-
linkedList1.add(22);
53-
linkedList1.add(33);
54-
linkedList1.add(44);
55-
linkedList1.add(55);
56-
linkedList1.add(66);
49+
linkedList1.add(0);
50+
linkedList1.add(1);
51+
linkedList1.add(2);
52+
linkedList1.add(3);
53+
linkedList1.add(4);
54+
linkedList1.add(5);
55+
linkedList1.add(6);
5756
linkedList1.printList();
5857
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
59-
linkedList2.add(00);
60-
linkedList2.add(11);
61-
linkedList2.add(22);
62-
linkedList2.add(33);
63-
linkedList2.add(44);
64-
linkedList2.add(55);
65-
linkedList2.add(66);
58+
linkedList2.add(0);
59+
linkedList2.add(1);
60+
linkedList2.add(2);
61+
linkedList2.add(3);
62+
linkedList2.add(4);
63+
linkedList2.add(5);
64+
linkedList2.add(6);
6665
linkedList2.printList();
6766
System.out.println(isIdentical(linkedList1, linkedList2));
6867
}

src/main/java/com/rampatra/linkedlists/IsPalindrome.java renamed to src/main/java/com/rampatra/linkedlists/Palindrome.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@
1010
*
1111
* @author rampatra
1212
* @since 6/18/15
13-
* @time: 2:35 PM
1413
*/
15-
public class IsPalindrome<E extends Comparable<E>> extends SingleLinkedList<E> {
14+
public class Palindrome<E extends Comparable<E>> extends SingleLinkedList<E> {
1615

1716
/**
1817
* Uses Stack to test whether a linked list starting
1918
* from {@param node} is palindrome or not.
2019
*
2120
* @param list
22-
* @return
21+
* @return {@code true} if linked list palindrome, {@code false} otherwise.
2322
*/
24-
public static <E extends Comparable<E>> boolean isPalindrome(SingleLinkedList<E> list) {
23+
private static <E extends Comparable<E>> boolean isPalindrome(SingleLinkedList<E> list) {
2524
SingleLinkedNode<E> head = list.getNode(0);
2625
SingleLinkedNode<E> curr = head;
2726
Stack<SingleLinkedNode<E>> stack = new LinkedStack<>();
@@ -52,7 +51,7 @@ public static <E extends Comparable<E>> boolean isPalindrome(SingleLinkedList<E>
5251
* @param node
5352
* @return
5453
*/
55-
public boolean isPalindromeRecursive(SingleLinkedNode<E> node) {
54+
private boolean isPalindromeRecursive(SingleLinkedNode<E> node) {
5655
if (node == null) return true;
5756

5857
boolean isPalindrome = isPalindromeRecursive(node.next);
@@ -65,13 +64,13 @@ public boolean isPalindromeRecursive(SingleLinkedNode<E> node) {
6564
}
6665
}
6766

68-
public static void main(String a[]) {
69-
IsPalindrome<Integer> linkedList = new IsPalindrome<>();
70-
linkedList.add(00);
71-
linkedList.add(11);
72-
linkedList.add(22);
73-
linkedList.add(11);
74-
linkedList.add(00);
67+
public static void main(String[] a) {
68+
Palindrome<Integer> linkedList = new Palindrome<>();
69+
linkedList.add(0);
70+
linkedList.add(1);
71+
linkedList.add(2);
72+
linkedList.add(1);
73+
linkedList.add(0);
7574
linkedList.printList();
7675
System.out.println(isPalindrome(linkedList));
7776
linkedList.printList();

0 commit comments

Comments
 (0)