Check if given LinkedList is a pallindrome or not
My version of the program. Open to corrections and improvements
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package interview.practice; | |
| import data.structures.LinkedList.LinkedList; | |
| import data.structures.LinkedList.Node; | |
| import data.structures.Stack.Stack; | |
| /** | |
| * | |
| * @author jasleen | |
| */ | |
| public class isPallindrome { | |
| public static boolean isPallindrome(LinkedList list) { | |
| Node single = list.head(), doubl = list.head(); | |
| Stack stack = new Stack(); | |
| if (single == null||single.next()==null) { | |
| return false; | |
| } | |
| while (single.next() != null && doubl.next().next() != null) { | |
| stack.push(single.key()); | |
| single = single.next(); | |
| doubl = doubl.next().next(); | |
| if ( doubl.next() == null) { | |
| break; | |
| } | |
| } | |
| System.out.println(doubl); | |
| System.out.println(single); | |
| System.out.println(stack); | |
| if (doubl.next()!= null) { | |
| stack.push(single.key()); | |
| single = single.next(); | |
| Node popped=stack.extractTop(); | |
| if (!single.key().toString().equals(popped.key().toString())) { | |
| System.out.println("Returning at key" + popped.key().toString()+single.key()); | |
| return false; | |
| } | |
| } | |
| single = single.next(); | |
| System.out.println(stack); | |
| //System.out.println(single.key()); | |
| while (single != null) { | |
| Node popped=stack.extractTop(); | |
| if (!single.key().toString().equals(popped.key().toString())) { | |
| System.out.println("Returning at key" + popped.key().toString()+single.key()); | |
| return false; | |
| } | |
| single = single.next(); | |
| } | |
| return true; | |
| } | |
| } |
Download code for this program here.
Download code for LinkedList here.
Download code for Node here.
Download code for Stack here.
Twitter
Facebook
Digg
Delicious
Stumble
0 comments:
Post a Comment