Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ListNode(x) {
- this.value = x;
- this.next = null;
- }
- function SinglyList(){
- this.head = null;
- }
- SinglyList.prototype.add = function(data) {
- currentNode = this.head;
- for (var i in data){
- var newNode = new ListNode(data[i]);
- if(currentNode == null){
- this.head = newNode;
- this._lenght++;
- currentNode = this.head;
- }
- else {
- currentNode.next = newNode;
- currentNode = currentNode.next;
- }
- }
- return this.head;
- };
- var arrayToLinkedlist = function(arr){
- if (arr == null) {
- return [];
- }
- if (!Array.isArray(arr)) {
- throw new TypeError('array-to-linkedlist expects an array.');
- }
- var singly = new SinglyList();
- return singly.add(arr);
- }
Add Comment
Please, Sign In to add comment