You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 2 | Detect cycle in a linkedlist |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/2.detectLoop/detectLoop.js)|[Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/2.detectLoop/detectLoop.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/2.detectLoop/detectLoop.js)| Easy | Floyd's cycle-finding or Tortoise and Hare algorithm |
| 4 | Merge K sorted lists |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/4.mergeKSortedLists/mergeKSortedLists.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/4.mergeKSortedLists/mergeKSortedLists.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/4.mergeKSortedLists/mergeKSortedLists.js)| Hard | Divide and conquer |
118
+
| 5 | Remove Kth node from end of list |[Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/5.removeKthNodeFromEnd/removeKthNodeFromEnd.js)|[JavaScript](https://livecodes.io/?console&xhttps://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/5.removeKthNodeFromEnd/removeKthNodeFromEnd.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/5.removeKthNodeFromEnd/removeKthNodeFromEnd.js)| Medium | Two pointers |
119
+
| 6 | Reorder list |[Source](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/6.reorderList/reorderList.js)|[Javascript](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/6.reorderList/reorderList.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/6.reorderList/reorderList.js)| Medium | Two pointers |
| 8 | Find Kth node from end of list |[Source](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/8.findKthNodeFromEnd/findKthNodeFromEnd.js)|[JavaScript](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/8.findKthNodeFromEnd/findKthNodeFromEnd.js)|[Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/8.findKthNodeFromEnd/findKthNodeFromEnd.md)| Easy | Two pointers |
122
+
| 9 | Partition list |[Source](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/9.partitionList/partitionList.js)|[JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/9.partitionList/partitionList.js)|[Documentation](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/9.partitionList/partitionList.md)| Medium | Two pointers |
| 11 | Binary to decimal | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/11.binaryToDecimal/binaryToDecimal.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/11.binaryToDecimal/binaryToDecimal.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/11.binaryToDecimal/binaryToDecimal.md) | Easy | List traversal and math operations |
113
125
114
-
2. Detect cycle in a linkedlist: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/detectLoop.js)
115
-
116
-
3. Merge two sorted lists: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/mergeTwoSortedLists.js)
117
-
118
-
4. Merge K sorted lists: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/mergeKSortedLists.js)
119
-
120
-
5. Remove Nth node from end of list: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/removeKthNodeFromEnd.js)
8. Find Kth node from end of list: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/findKthNodeFromEnd.js)
11. Binary to decimal: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/binaryToDecimal.js)
Given a `head` of singly linked list. The linked list holds the binary representation of a decimal number using either 0 or 1 digit. Calculate the decimal value of the linked list and return the number.
3
+
4
+
**Note:** The MSB(Most Significant Bit) is situated at the head of linkedlist.
This problem is solved with the help of linked list iteration and mathematic calculations. The algorithmic approach can be summarized as follows:
23
+
24
+
1. Create a current node(`current`) to traverse the list. It is initialized with head node.
25
+
26
+
2. Create a decimal variable(`decimal`) to store the decimal equivalent and initialized to zero.
27
+
28
+
3. Loop over the list until the current pointer is not null. The loop starts from left to right.
29
+
30
+
4. In each iteration, update the decimal value by adding the previous decimal value and the product of power of 2 with current binary digit.
31
+
32
+
5. Update the current node to its next element for each iteration.
33
+
34
+
6. Return the decimal value as equivalent binary number.
35
+
36
+
**Time and Space complexity:**
37
+
This algorithm takes a time complexity of `O(n)`, where `n` is the number of nodes in the list. This is because we needs to traverse at most once to calculate the decimal equivalent.
38
+
39
+
Here, we don't use any additional datastructure other than one pointer variable. Hence, the space complexity will be `O(1)`.
0 commit comments