Guest User

Untitled

a guest
Nov 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function ListNode(x) {
  2. this.value = x;
  3. this.next = null;
  4. }
  5.  
  6. function SinglyList(){
  7. this.head = null;
  8. }
  9.  
  10. SinglyList.prototype.add = function(data) {
  11. currentNode = this.head;
  12. for (var i in data){
  13. var newNode = new ListNode(data[i]);
  14. if(currentNode == null){
  15. this.head = newNode;
  16. this._lenght++;
  17. currentNode = this.head;
  18. }
  19. else {
  20. currentNode.next = newNode;
  21. currentNode = currentNode.next;
  22. }
  23. }
  24. return this.head;
  25. };
  26.  
  27. var arrayToLinkedlist = function(arr){
  28. if (arr == null) {
  29. return [];
  30. }
  31.  
  32. if (!Array.isArray(arr)) {
  33. throw new TypeError('array-to-linkedlist expects an array.');
  34. }
  35.  
  36. var singly = new SinglyList();
  37. return singly.add(arr);
  38. }
Add Comment
Please, Sign In to add comment