c++ - How to check if double linked list is a pallindrome without using extra space? -
recently went interview , asked me "to check if below double linked list pallindrome without using storage stl's, linked-list, stack, queue, tree, string, character arrays. etc.." unable give perfect solution though.
below image view.
this not homework question merely question find solutions shared.
declare 2 iterators, start , end. loop through list , decrement/increment them simultaneously, comparing @ each step. note: algorithm assumes have overridden operators, works sort of list, not numerical lists.
for(int i=0;i<list.size()/2;i++){ if(*start!=*end) return false; start++; end--; } return true;
the key here using iterators instead of working list directly.
Comments
Post a Comment