Code coverage report for lib/algorithms/2-linkedLists/palindrome.js

Statements: 100% (17 / 17)      Branches: 100% (6 / 6)      Functions: 100% (1 / 1)      Lines: 100% (17 / 17)      Ignored: none     

All files » lib/algorithms/2-linkedLists/ » palindrome.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31    1   1   3 3 3   3 10 10 10       3 1     3 8 1 7     2     1
//2.7 Given a linked list, determine if you have a palindrome (same backwords or forwards)
 
var stack = require("../../../lib/dataStructures/stack.js");
 
var palindrome = function(head) {
 
	var myStack = new stack();
	var slow = head;
	var fast = head;
 
	while((fast !== null) && (fast.next !== null)) {
		myStack.push(slow.data);
		slow = slow.next;
		fast = fast.next.next;
	}
 
	//this case covers an odd-numbered list
	if(fast !== null) {
		slow = slow.next;
	}
 
	while(slow !== null) {
		if(slow.data !== myStack.pop())
			return false;
		slow = slow.next;
	}
 
	return true;
};
 
module.exports = palindrome;