|
| 1 | +package algorithm.basic03; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +/** |
| 6 | + * @Created by mood321 |
| 7 | + * @Date 2019/10/28 0028 |
| 8 | + * @Description TODO |
| 9 | + */ |
| 10 | +public class Code_11_IsPalindromeList { |
| 11 | + public static class Node { |
| 12 | + int data; |
| 13 | + Node next; |
| 14 | + |
| 15 | + Node(int data) { |
| 16 | + this.data = data; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + // need n extra space |
| 21 | + public static boolean isPalindrome1(Node head) { |
| 22 | + if (head == null || head.next ==null){ |
| 23 | + return false; |
| 24 | + } |
| 25 | + Stack<Node> stack = new Stack<>(); |
| 26 | + Node cur = head; |
| 27 | + while (cur != null) { |
| 28 | + stack.push(cur); |
| 29 | + cur = cur.next; |
| 30 | + } |
| 31 | + while (head != null) { |
| 32 | + if (head.data != stack.pop().data) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + head = head.next; |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + // need n/2 extra space |
| 41 | + public static boolean isPalindrome2(Node head) { |
| 42 | + if (head == null || head.next ==null){ |
| 43 | + return false; |
| 44 | + } |
| 45 | + Node left = head; |
| 46 | + Node right = head; |
| 47 | + while (right.next != null && right.next.next != null) { |
| 48 | + left = left.next; |
| 49 | + right = right.next.next; |
| 50 | + } |
| 51 | + Stack<Node> stack = new Stack<>(); |
| 52 | + while (left != null) { |
| 53 | + stack.push(left); |
| 54 | + left = left.next; |
| 55 | + } |
| 56 | + while (!stack.isEmpty()) { |
| 57 | + if (stack.pop().data != head.data) { |
| 58 | + return false; |
| 59 | + |
| 60 | + } |
| 61 | + head = head.next; |
| 62 | + } |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + // need O(1) extra space |
| 67 | + public static boolean isPalindrome3(Node head) { |
| 68 | + if (head == null || head.next == null) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + Node left = head; |
| 72 | + Node right = head; |
| 73 | + while (right.next != null && right.next.next != null) { // find mid node |
| 74 | + left = left.next; // left -> mid |
| 75 | + right = right.next.next; // right -> end |
| 76 | + } |
| 77 | + right = left.next; // right -> right part first node |
| 78 | + left.next = null; // mid.next -> null |
| 79 | + Node tem = null; |
| 80 | + while (right != null) { // right part convert |
| 81 | + tem = right.next; // tem -> save next node |
| 82 | + right.next = left; // next of right node convert |
| 83 | + left = right; // left move |
| 84 | + right = tem; // right move |
| 85 | + } |
| 86 | + tem = left; // tem -> save last node |
| 87 | + right = head;// right -> left first node |
| 88 | + boolean res = true; |
| 89 | + while (left != null && right != null) { // check palindrome |
| 90 | + if (left.data != right.data) { |
| 91 | + res = false; |
| 92 | + break; |
| 93 | + } |
| 94 | + left = left.next; // left to mid |
| 95 | + right = right.next; // right to mid |
| 96 | + } |
| 97 | + left = tem.next; |
| 98 | + tem.next = null; |
| 99 | + while (left != null) { // recover list |
| 100 | + right = left.next; |
| 101 | + left.next = tem; |
| 102 | + tem = left; |
| 103 | + left = right; |
| 104 | + } |
| 105 | + return res; |
| 106 | + } |
| 107 | + |
| 108 | + private static Node reverse(Node left1,Node stop) { |
| 109 | + Node left=left1; |
| 110 | + Node right = left.next; |
| 111 | + left.next = null; |
| 112 | + Node tem = null; |
| 113 | + while (right != null) { |
| 114 | + if(left==stop){ |
| 115 | + break; |
| 116 | + } |
| 117 | + tem = right.next; |
| 118 | + right.next = left; |
| 119 | + left = right; |
| 120 | + right = tem; |
| 121 | + } |
| 122 | + return left; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + public static void printLinkedList(Node node) { |
| 127 | + System.out.print("Linked List: "); |
| 128 | + while (node != null) { |
| 129 | + System.out.print(node.data + " "); |
| 130 | + node = node.next; |
| 131 | + } |
| 132 | + System.out.println(); |
| 133 | + } |
| 134 | + |
| 135 | + public static void main(String[] args) { |
| 136 | + |
| 137 | + Node head = null; |
| 138 | + /* printLinkedList(head); |
| 139 | + System.out.print(isPalindrome1(head) + " | "); |
| 140 | + System.out.print(isPalindrome2(head) + " | "); |
| 141 | + System.out.println(isPalindrome3(head) + " | "); |
| 142 | + printLinkedList(head); |
| 143 | + System.out.println("========================="); |
| 144 | +
|
| 145 | + head = new Node(1); |
| 146 | + printLinkedList(head); |
| 147 | + System.out.print(isPalindrome1(head) + " | "); |
| 148 | + System.out.print(isPalindrome2(head) + " | "); |
| 149 | + System.out.println(isPalindrome3(head) + " | "); |
| 150 | + printLinkedList(head); |
| 151 | + System.out.println("========================="); |
| 152 | +
|
| 153 | + head = new Node(1); |
| 154 | + head.next = new Node(2); |
| 155 | + printLinkedList(head); |
| 156 | + System.out.print(isPalindrome1(head) + " | "); |
| 157 | + System.out.print(isPalindrome2(head) + " | "); |
| 158 | + System.out.println(isPalindrome3(head) + " | "); |
| 159 | + printLinkedList(head); |
| 160 | + System.out.println("========================="); |
| 161 | +
|
| 162 | + head = new Node(1); |
| 163 | + head.next = new Node(1); |
| 164 | + printLinkedList(head); |
| 165 | + System.out.print(isPalindrome1(head) + " | "); |
| 166 | + System.out.print(isPalindrome2(head) + " | "); |
| 167 | + System.out.println(isPalindrome3(head) + " | "); |
| 168 | + printLinkedList(head); |
| 169 | + System.out.println("========================="); |
| 170 | +
|
| 171 | + head = new Node(1); |
| 172 | + head.next = new Node(2); |
| 173 | + head.next.next = new Node(3); |
| 174 | + printLinkedList(head); |
| 175 | + System.out.print(isPalindrome1(head) + " | "); |
| 176 | + System.out.print(isPalindrome2(head) + " | "); |
| 177 | + System.out.println(isPalindrome3(head) + " | "); |
| 178 | + printLinkedList(head); |
| 179 | + System.out.println("========================="); |
| 180 | +
|
| 181 | + head = new Node(1); |
| 182 | + head.next = new Node(2); |
| 183 | + head.next.next = new Node(1); |
| 184 | + printLinkedList(head); |
| 185 | + System.out.print(isPalindrome1(head) + " | "); |
| 186 | + System.out.print(isPalindrome2(head) + " | "); |
| 187 | + System.out.println(isPalindrome3(head) + " | "); |
| 188 | + printLinkedList(head); |
| 189 | + System.out.println("========================="); |
| 190 | +
|
| 191 | + head = new Node(1); |
| 192 | + head.next = new Node(2); |
| 193 | + head.next.next = new Node(3); |
| 194 | + head.next.next.next = new Node(1); |
| 195 | + printLinkedList(head); |
| 196 | + System.out.print(isPalindrome1(head) + " | "); |
| 197 | + System.out.print(isPalindrome2(head) + " | "); |
| 198 | + System.out.println(isPalindrome3(head) + " | "); |
| 199 | + printLinkedList(head); |
| 200 | + System.out.println("========================="); |
| 201 | +
|
| 202 | + head = new Node(1); |
| 203 | + head.next = new Node(2); |
| 204 | + head.next.next = new Node(2); |
| 205 | + head.next.next.next = new Node(1); |
| 206 | + printLinkedList(head); |
| 207 | + System.out.print(isPalindrome1(head) + " | "); |
| 208 | + System.out.print(isPalindrome2(head) + " | "); |
| 209 | + System.out.println(isPalindrome3(head) + " | "); |
| 210 | + printLinkedList(head); |
| 211 | + System.out.println("========================="); |
| 212 | +*/ |
| 213 | + head = new Node(1); |
| 214 | + head.next = new Node(2); |
| 215 | + head.next.next = new Node(3); |
| 216 | + head.next.next.next = new Node(2); |
| 217 | + head.next.next.next.next = new Node(1); |
| 218 | + printLinkedList(head); |
| 219 | + System.out.print(isPalindrome1(head) + " | "); |
| 220 | + System.out.print(isPalindrome2(head) + " | "); |
| 221 | + System.out.println(isPalindrome3(head) + " | "); |
| 222 | + printLinkedList(head); |
| 223 | + System.out.println("========================="); |
| 224 | + |
| 225 | + } |
| 226 | +} |
0 commit comments