{"id":16157970,"url":"https://github.com/can-dy-jack/linkedlist","last_synced_at":"2025-04-07T01:45:59.622Z","repository":{"id":65190321,"uuid":"586449361","full_name":"can-dy-jack/linkedlist","owner":"can-dy-jack","description":"LinkedList and DoubleLinkedList implementation in JavaScript","archived":false,"fork":false,"pushed_at":"2023-01-11T04:45:54.000Z","size":71,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T10:17:01.735Z","etag":null,"topics":["algorithm","data-structures","double-linked-list","javascript","linked-list"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/can-dy-jack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-08T07:03:22.000Z","updated_at":"2023-03-07T03:47:14.000Z","dependencies_parsed_at":"2023-02-09T01:45:21.741Z","dependency_job_id":null,"html_url":"https://github.com/can-dy-jack/linkedlist","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Flinkedlist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Flinkedlist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Flinkedlist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/can-dy-jack%2Flinkedlist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/can-dy-jack","download_url":"https://codeload.github.com/can-dy-jack/linkedlist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247578615,"owners_count":20961270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","data-structures","double-linked-list","javascript","linked-list"],"created_at":"2024-10-10T01:51:42.560Z","updated_at":"2025-04-07T01:45:59.584Z","avatar_url":"https://github.com/can-dy-jack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @kartjim/linkedlist\nLinkedList and DoubleLinkedList implementation in JavaScript\n\n\u003e 学习项目，参考自：[datastructures-js/linked-list](https://github.com/datastructures-js/linked-list)\n\n\u003c!-- 代码注释 @params @returns --\u003e\n\u003c!-- .d.ts 文件 --\u003e\n\n## install\n\n```sh\nnpm i @kartjim/linkedlist\n```\n\n## require\n```js\nconst {\n  LinkedList,\n  DoubleLinkedList,\n  LinkedListNode,\n  DoubleLinkedListNode,\n} = require('@kartjim/linkedlist');\n```\n\n## import\n```js\nimport {\n  LinkedList,\n  DoubleLinkedList,\n  LinkedListNode,\n  DoubleLinkedListNode,\n} from '@kartjim/linkedlist';\n```\n## LinkedList API\n### use\nconstructor\n\n```js\nconst root = new LinkedList();\n```\n### insertHead\ninserts a node at the beginning of the list.  \ntime complexity: $O(1)$\n\n```js\nroot.insertHead(12)\nconsole.log(root.insertHead(11).getCount()) // 11\nconsole.log(root.getCount()) // 2\nconsole.log(root.toArray()) // [11, 12]\n```\n\n### insertTail\nadd a node at the end of the LinkedList.  \ntime complexity: min $O(1)$ , max $O(n)$\n```js\nlet saveNode = root.insertTail(21);\nroot.insertTail(22, saveNode);\nconsole.log(root.getCount()) // 4\nconsole.log(root.toArray()) // [11, 12, 21, 22]\n```\n\n### insertAt\nadd a node at a specify location.  \ntime complexity: $O(n)$\n```js\nroot.insertAt(2, 31)\nconsole.log(root.getCount()) // 5\nconsole.log(root.toArray()) // [11, 12, 31, 21, 22]\n```\n\n### getHead\nget the head node.  \n```js\nconsole.log(root.getHead().getValue()) // 11\n```\n### getCount\nget the nodes count.  \n```js\nconsole.log(root.getCount()) // 5\n```\n\n### isEmpty\ncheck if the likedlist is empty.  \n```js\nconsole.log(root.isEmpty()) // false\n```\n\n### toArray\nconvert linkedlist into Array.  \ntime complexity: $O(n)$\n```js\nconsole.log(root.toArray()) // [11, 12, 31, 21, 22]\n```\n### reverse\nreverse the linkedlist.  \n\n```js\nroot.reverse();\n// [12, 13, 42, 43]\n// =\u003e \n// [43, 42, 13, 12]\n```\n\n### removeHead\nremove a node at the beginnig of the LinkedList.  \ntime complexity: $O(1)$\n```js\nroot.removeHead()\nconsole.log(root.getHead().getValue()) // 12\n```\n\n### removeTail\nremove a node at the end of the LinkedList.  \ntime complexity: $O(n)$\n```js\nroot.removeTail()\nconsole.log(root.getCount()) // 3\nconsole.log(root.toArray()) // [12, 31, 21]\n```\n\n### removeAt\nremove a node at a specify location. \n\u003e First (head) node is at position 0.  \n\ntime complexity: $O(n)$\n```js\nroot.removeAt(1)\nconsole.log(root.getCount()) // 2\nconsole.log(root.toArray()) // [12, 21]\n```\n\n### removeEach\nremove nodes based on a callback.  \n```js\nroot.insertTail(41); root.insertTail(42);\nconsole.log(root.toArray()) // [12, 21, 41, 42]\nroot.removeEach(\n    (n) =\u003e n.getValue() \u003c 41 \u0026\u0026 n.getValue() \u003e 12\n)\nconsole.log(root.toArray()) // [12, 41, 42]\n```\n### forEach\nTraverses the list from beginning to end. The original `root` will not be changed.  \n\n```js\nconst arr = [];\nroot.forEach((node) =\u003e {\n    const val = node.getValue();\n    if (val \u003e 40) arr.push(val);\n});\nconsole.log(arr) // [41, 42]\n```\n\n### find\nfind the first node in the linkedlist based on a callback, get null if nothing is found. It also accepts a second param as the starting node to start searching from. \n\n```js\nconst num1 = root.find((n) =\u003e n.getValue() === 41);\nconst num2 = root.find((n) =\u003e n.getValue() === 21);\nconsole.log(num1.getValue()) // 41\nconsole.log(num2) // null\n```\n### filter\nfilter the linkedlist based on a callback, return a new LinkedList.  \n\n```js\nconsole.log(root.filter((n) =\u003e n.getValue() \u003e 40).toArray()) // [41, 42]\n```\n### map\nmap the linkedlist\\' value based on a callback, return a new LinkedList. \n\n```js\nroot.toArray()    // [12, 41, 42]);\nconst newRoot = root.map((n) =\u003e n % 2);\nnewRoot.toArray()  // [0, 1, 0]\nroot === newRoot  // false\n```\n### reduce\nreduce the linkedlist based on a callback, return value.\n\n```js\nconst newRoot = root.reduce((pre, cur) =\u003e cur + pre, 0); \n// [12, 41, 42] =\u003e 95\n```\n\n### insertBefore\nAdd a node before the head node.   \ntime complexity: $O(n)$\n```js\nconst Twelve = root.find((n) =\u003e n.getValue() === 12);\nroot.insertBefore(Twelve, 1)\nconsole.log(root.toArray()) // [1, 12, 41, 42] \n```\n\n### insertAfter\nAdd a node after the giving node. \ntime complexity: $O(1)$\n```js\nconst Thirteen = root.find((n) =\u003e n.getValue() === 13);\nroot.insertAfter(Thirteen, 14)\n// [1, 12, 13, 41, 42] \n// =\u003e \n// [1, 12, 13, 14 41, 42] \n```\n\n### removeBefore\nremove nodes before the giving node.  \ntime complexity: $O(n)$\n```js\nconst FortyOne = root.find((n) =\u003e n.getValue() === 41);\nroot.removeBefore(FortyOne)\n// [12, 13, 14, 41, 42, 43]\n// =\u003e \n// [12, 13, 41, 42, 43]\n```\n\n### removeAfter\nremove nodes after the giving node.  \ntime complexity: $O(1)$\n```js\nconst Thirteen = root.find((n) =\u003e n.getValue() === 13);\nroot.removeAfter(Thirteen)\n// [12, 13, 41, 42, 43]\n// =\u003e \n// [12, 13, 42, 43]\n```\n\n### clear\nclear the linkedlist. \n\n```js\nroot.clear();\nconsole.log(root.getCount()) // 0\nconsole.log(root.getHead()) // null\n```\n### LinkedList.fromArray\ncreate a LinkedList from an Array.  \n\n```js\nconst root2 = LinkedList.fromArray([1, 2, 3, 4, 5]);\n```\n\n## LinkedListNode\n\n### setValue\nset the value of the node.\n\n### getValue\nget the value of the node.\n\n### setNext\nset the next node.\n\n### getNext\nget the next node.\n\n### hasNext\nchecks if node has a next node.\n\n## DoubleLinkedList API\n### use\nconstructor\n\n```js\nconst root = new DoubleLinkedList();\n```\n\n### insertHead\ninserts a node at the beginning of the DoubleLinkedList.  \ntime complexity: $O(1)$\n\n```js\nroot.insertHead(2)\nconsole.log(root.insertHead(1).getCount()) // 1\nconsole.log(root.getCount()) // 2\nconsole.log(root.toArray()) // [1, 2]\n```\n\n### insertTail\nadd a node at the end of the DoubleLinkedList.  \ntime complexity: $O(1)$ \n```js\nroot.insertTail(3)\nconsole.log(root.getCount()) // 3\nconsole.log(root.toArray()) // [1, 2, 3]\n```\n### insertAt\nadd a node at a specific position.  \ntime complexity: $O(n)$ \n```js\nroot.insertAt(0, 0)\nroot.insertAt(4, 4)\nroot.insertAt(2, 21)\nconsole.log(root.getCount()) // 6\nconsole.log(root.toArray()) // [0, 1, 21, 2, 3, 4]\n```\n\n### getCount\nget the count of the DoubleLinkedList.  \n \n```js\nconsole.log(root.getCount()) // 6\n```\n\n### toArray\nconvert the doubleLinkedList into an array.\n \n```js\nconsole.log(root.toArray()) // [0, 1, 21, 2, 3, 4]\n```\n### isEmpty\ncheck if the DoubleLinkedList is empty.  \n \n```js\nconsole.log(root.isEmpty()) // false\n```\n\n### getHead\nget the head of doubleLinkedList.    \n \n```js\nconsole.log(root.getHead().getValue()) // 0\n```\n### getTail\nget the tail of doubleLinkedList.    \n \n```js\nconsole.log(root.getTail().getValue()) // 4\n```\n\n### forEach\nTraverses the list from beginning to end.     \n \n```js\nconst arr = [];\nroot.forEach((node) =\u003e {\n  const val = node.getValue();\n  if (val \u003e 9) arr.push(val);\n});\nconsole.log(arr) // [21]\n```\n### removeHead\nremove the head of the DoubleLinkedList.      \ntime complexity: $O(1)$\n```js\nconsole.log(root.removeHead().getValue()) // 0\nconsole.log(root.toArray()) // [1, 21, 2, 3, 4]\n```\n### removeTail\nremove the tail of the DoubleLinkedList.      \ntime complexity: $O(1)$\n```js\nconsole.log(root.removeTail().getValue()) // 4\nconsole.log(root.toArray()) // [1, 21, 2, 3]\n```\n\n### removeAt\nremove a node at a specific position.       \ntime complexity: $O(n)$\n```js\nconsole.log(root.removeAt(2).getValue()) // 2\nconsole.log(root.toArray()) // [1, 21, 3]\n```\n\n### find     \nfind the first node in the DoubleLinkedList based on a callback, get null if nothing is found. It also accepts a second param as the starting node to start searching from. \n```js\nconst num2 = root.find((n) =\u003e n.getValue() === 21);\nconsole.log(num2.getValue()) // 21\n```\n### filter     \nfilter the linkedlist based on a callback, return a new LinkedList.  \n```js\nconsole.log(root.filter((n) =\u003e n.getValue() \u003e 20).toArray()) // [21]\n```\n\n### clear     \nclear the linkedlist.  \n```js\nroot.clear();\nconsole.log(root.getCount()) // 0\nconsole.log(root.isEmpty()) // true\n```\n\n### DoubleLinkedList.fromArray()     \ncreate a LinkedList from an Array.   \n```js\nconsole.log(DoubleLinkedList.fromArray(['a', 'b', 'c', 'd']).toArray())\n// ['a', 'b', 'c', 'd']\n```\n## DoubleLinkedListNode\n### setValue\nset the value of the node.\n\n### getValue\nget the value of the node.\n\n### setPrev\nset the previous node.\n\n### getPrev\nget the previous node.\n\n### hasPrev\ncheck if node has a previous node.\n\n### setNext\nset the next node.\n\n### getNext\nget the next node.\n\n### hasNext\ncheck if node has a next node.\n\n## License\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcan-dy-jack%2Flinkedlist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcan-dy-jack%2Flinkedlist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcan-dy-jack%2Flinkedlist/lists"}