Last active
August 29, 2015 14:13
-
-
Save arunsivasankaran/93b1faeb3135adc73e26 to your computer and use it in GitHub Desktop.
Course Hero LinkdList Solution.
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
/* | |
input: A -> B -> C -// | |
output: C -> B -> A -// | |
*/ | |
var LinkdList = function (item) { | |
this.name = item; | |
this.next = null; | |
}; | |
// Set next item in LinkedList | |
LinkdList.prototype.nextItem = function (nitem) { | |
var _this = this; | |
if (_this.next !== null) { _this = _this.next}; | |
_this.next = new LinkdList(nitem); | |
return _this.next; | |
}; | |
var A = new LinkdList("a"); | |
var B = A.nextItem("b"); | |
var C = A.nextItem("c"); | |
// A -> B -> C | |
// C -> B -> A | |
// Reverse Link relationship between items | |
LinkdList.prototype.reverseLink = function (item) { | |
var ele = item; | |
var currEle = item; | |
while (ele.next !== null ) { | |
if (ele.next !== null) { | |
ele.next.reverse = ele; | |
} | |
currEle = ele.next; | |
ele.next = ele.reverse || null; | |
delete ele.reverse; | |
ele = currEle; | |
} | |
ele.next = ele.reverse; | |
delete ele.reverse; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment