Skip to content

Instantly share code, notes, and snippets.

@arunsivasankaran
Last active August 29, 2015 14:13
Show Gist options
  • Save arunsivasankaran/93b1faeb3135adc73e26 to your computer and use it in GitHub Desktop.
Save arunsivasankaran/93b1faeb3135adc73e26 to your computer and use it in GitHub Desktop.
Course Hero LinkdList Solution.
/*
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