java - how to turn a linked list data into a number? -
i have question linked list. each object has 2 variables: pointer next object, , own value. point of question check whether number palindrome. i'm trying convert numbers ( in order given) n variable in order reverse check i've failed far, either overflowing long\int variables or having decimal point(.) , exx input after first digit, if using double.
this current code:
public class driver { // driver program public static void main(string args[]) { int count=12; double number=0; double x=1; while(count>0) { number=number+x*(math.pow(10,count)); system.out.println(number); count--; if(x<10) x++; else x=1; } system.out.println(num); } i assume there's easier way build number except pow?
your help( , future ideas regarding palindrome problem , linked lists) appreciate.
copy elements stack, compare elements in list elements in stack. pseudocode:
node = list.head; while (node != null) { stack.push(node.value); node = node.next; } node = list.head; boolean palindrome = true; while (node != null) { if (stack.pop != node.value) { palindrome = false; break; } } if (palindrome) { print "palindrome" } else { print "not palindrome" }
Comments
Post a Comment