-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from TiaLow/master
Function to determine if a linked list is a palindrome
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
// JavaScript implementation of a function that finds the number of leaves in a binary tree | ||
// function's signature is a binary tree as a parameter, returns the number of leaves in the tree | ||
// relies on the assumption that the input is a binary tree with a root, and made up of nodes with left and right properties on them, as illustrated below | ||
// function uses preOrder depth traversal method | ||
|
||
class Node { | ||
|
||
constructor(value, left = null, right = null) { | ||
this.value = value; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
} | ||
|
||
class BinaryTree { | ||
|
||
constructor( root = null) { | ||
this.root = root; | ||
} | ||
|
||
} | ||
|
||
function numLeavesInTree(binaryTree) { | ||
|
||
let numLeaves = 0; | ||
|
||
function _preOrderTraversal(root) { | ||
|
||
if(!root) { | ||
return; | ||
} | ||
|
||
numLeaves += 1; | ||
_preOrderTraversal(root.left); | ||
_preOrderTraversal(root.right); | ||
|
||
} | ||
|
||
_preOrderTraversal(binaryTree.root); | ||
return numLeaves; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
// Javascript implementation of a stand alone function that determines if a linked list is a palindrome | ||
// function's signature is a linked list as a parameter and returns true if it's a palindrome, false if not | ||
// relies on a linked list node implementation as written below | ||
|
||
class Node { | ||
|
||
constructor(value, next = null) { | ||
this.value = value; | ||
this.next = next; | ||
} | ||
} | ||
|
||
|
||
class LinkedList { | ||
|
||
constructor() { | ||
this.head = null; | ||
} | ||
|
||
insert(value) { | ||
this.head = new Node(value, this.head); | ||
} | ||
|
||
} | ||
|
||
function determineListPalindrome(linkedList) { | ||
|
||
let arr =[]; | ||
|
||
let currentNode = linkedList.head; | ||
|
||
if(currentNode) { | ||
arr.push(currentNode.value); | ||
} | ||
|
||
while(currentNode.next) { | ||
currentNode = currentNode.next; | ||
arr.push(currentNode.value); | ||
} | ||
|
||
for(let i = 0; i < arr.length/2; i++) { | ||
|
||
if(arr[i] !== arr[arr.length - ( i + 1 ) ] ){ | ||
|
||
return false; | ||
|
||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
|