Skip to content

Commit cceb91b

Browse files
committed
Update linkedlist algorithms
1 parent a6c4e6a commit cceb91b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1504
-444
lines changed

README.md

+13-20
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,20 @@ List of Programs related to data structures and algorithms
109109

110110
### LinkedList
111111

112-
1. Reverse substring: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/1.reverseSublist/reverseSublist.js)
112+
| No. | Name | Source | Playground | Documentation | Level | Pattern |
113+
| :-: | :------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----: | :--------------------------------------------------: |
114+
| 1 | Reverse sublist | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/1.reverseSublist/reverseSublist.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/1.reverseSublist/reverseSublist.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/1.reverseSublist/reverseSublist.js) | Easy | List traversal |
115+
| 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 |
116+
| 3 | Merge two sorted lists | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/3.mergeTwoSortedLists/mergeTwoSortedLists.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/mergeTwoSortedLists.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/3.mergeTwoSortedLists/mergeTwoSortedLists.md) | Easy | Arithmetic comparison |
117+
| 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 |
120+
| 7 | Find middle node | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/7.findMiddleNode/findMiddleNode.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/7.findMiddleNode/findMiddleNode.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/7.findMiddleNode/findMiddleNode.md) | Easy | Two pointers |
121+
| 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 |
123+
| 10 | Remove duplicates | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/10.removeDuplicates/removeDuplicates.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/10.removeDuplicates/removeDuplicates.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/10.removeDuplicates/removeDuplicates.md) | Easy | Two pointers |
124+
| 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 |
113125

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)
121-
122-
6. Reorder list: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/reorderList.js)
123-
124-
7. Find middle node: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/findMiddleNode.js)
125-
126-
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)
127-
128-
9. Partition list: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/partitionList.js)
129-
130-
10. Remove duplicates: [JavaScript](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/removeDuplicates.js)
131-
132-
11. Binary to decimal: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/linkedlist/binaryToDecimal.js)
133126

134127

135128
### Doubly linkedlist

src/images/binary-to-decimal.png

13.9 KB
Loading

src/images/find-middle-node.png

22.8 KB
Loading

src/images/kth-node-from-end.png

18.9 KB
Loading

src/images/merge-linkedlist.png

25.2 KB
Loading

src/images/partition-list.png

29.9 KB
Loading

src/images/remove-duplicates.png

20.6 KB
Loading
17.9 KB
Loading

src/images/reorder-list.png

18.8 KB
Loading

src/java1/algorithms/linkedlist/FindKthNodeFromEnd.java

-40
This file was deleted.

src/java1/algorithms/linkedlist/FindMiddleNode.java

-39
This file was deleted.

src/java1/algorithms/linkedlist/MergeTwoSortedLists.java

-78
This file was deleted.

src/java1/algorithms/linkedlist/Node.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Node {
55
public Node next;
66
public int value;
77

8-
Node(int value) {
8+
public Node(int value) {
99
this.value = value;
1010
this.next = null;
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package java1.algorithms.linkedlist.binaryToDecimal;
2+
3+
import java1.algorithms.linkedlist.Node;
4+
5+
public class BinaryToDecimal {
6+
private static int binaryToDecimal(Node head, int length){
7+
Node current = head;
8+
int decimal = 0;
9+
10+
for (int i= length-1; i>=0; i--) {
11+
decimal += Math.pow(2, i) * current.value;
12+
current = current.next;
13+
}
14+
15+
return decimal;
16+
}
17+
18+
private static int binaryToDecimalDoubling(Node head){
19+
Node current = head;
20+
int decimal = 0;
21+
22+
while(current != null) {
23+
decimal = decimal * 2 + current.value;
24+
current = current.next;
25+
}
26+
27+
return decimal;
28+
}
29+
30+
public static void main(String[] args) {
31+
Node headNode1 = new Node(1);
32+
Node node1 = new Node(0);
33+
Node node2 = new Node(0);
34+
Node node3 = new Node(1);
35+
36+
headNode1.setNext(node1);
37+
node1.setNext(node2);
38+
node2.setNext(node3);
39+
40+
Node headNode2 = new Node(1);
41+
Node node4 = new Node(0);
42+
Node node5 = new Node(1);
43+
Node node6 = new Node(1);
44+
45+
headNode2.setNext(node4);
46+
node4.setNext(node5);
47+
node5.setNext(node6);
48+
49+
System.out.println(binaryToDecimalDoubling(headNode1));
50+
System.out.println(binaryToDecimalDoubling(headNode2));
51+
52+
System.out.println(binaryToDecimal(headNode1, 4));
53+
System.out.println(binaryToDecimal(headNode2, 4));
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Problem statement:**
2+
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.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: [1,0,0,1]
10+
Output: 9
11+
12+
Example 2:
13+
14+
Input: [1,0,1,1]
15+
Output: 11
16+
17+
## Pictorial representation
18+
19+
![Screenshot](../../../../images/binary-to-decimal.png)
20+
21+
**Algorithmic Steps**
22+
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

Comments
 (0)