Skip to content

Commit b0a12e5

Browse files
committed
Update doubly linked list datastructures and problems
1 parent 9619bcd commit b0a12e5

File tree

26 files changed

+910
-81
lines changed

26 files changed

+910
-81
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ List of Programs related to data structures and algorithms
1717
1. SinglyLinkedlist implementation: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/singlyLinkedList/singlyLinkedList.js) [Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/singlyLinkedList/singlyLinkedList.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/singlyLinkedList/singlyLinkedList.md)
1818

1919
### DoublyLinkedlist
20-
1. DoublyLinkedlist implementation: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/doublyLinkedlist/class/doublyLinkedlist.js)
20+
21+
1. DoublyLinkedlist implementation: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/doublyLinkedlist/doublyLinkedlist.js) [Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/doublyLinkedlist/doublyLinkedlist.js) [Documentation](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/doublyLinkedlist/doublyLinkedlist.md)
2122

2223
### Tree
2324
1. Binary Search Tree: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/datastructures/trees/binary_search_tree.js)
@@ -127,11 +128,11 @@ List of Programs related to data structures and algorithms
127128

128129
### Doubly linkedlist
129130

130-
1. Swap first and last: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/swapFirstAndLast.js)
131-
132-
2. Palindrome check: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/palindromeCheck.js)
133-
134-
3. Swap node pairs: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/SwapNodePairs.js)
131+
| No. | Name | Source | Playground | Documentation | Level | Pattern |
132+
| :-: | :------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----: | :--------------------------------------------------: |
133+
| 1 | Swap first and last | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/1.swapFirstAndLast/swapFirstAndLast.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/1.swapFirstAndLast/swapFirstAndLast.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/1.swapFirstAndLast/swapFirstAndLast.md) | Easy | Swap nodes |
134+
| 2 | Palindrome check | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/2.palindromeCheck/palindromeCheck.js) | [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/2.palindromeCheck/palindromeCheck.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/2.palindromeCheck/palindromeCheck.md) | Easy | Two pointers |
135+
| 3 | Swap node pairs | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/3.SwapNodePairs/SwapNodePairs.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/3.SwapNodePairs/SwapNodePairs.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/doublyLinkedlist/3.SwapNodePairs/SwapNodePairs.md) | Medium | List traversal and update pointers |
135136

136137
### Tree
137138

src/images/doubly-linked-list.png

10.6 KB
Loading

src/images/doubly-linkedlist.png

9.13 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package java1.algorithms.doublyLinkedlist.palindromeCheck;
2+
3+
class Node {
4+
public int value;
5+
public Node next;
6+
public Node prev;
7+
8+
Node(int value){
9+
this.value = value;
10+
this.next = null;
11+
this.prev = null;
12+
}
13+
}
14+
15+
class DoublyLinkedlist {
16+
17+
private Node head;
18+
private Node tail;
19+
private int length;
20+
21+
DoublyLinkedlist(){
22+
this.head = null;
23+
this.tail = null;
24+
this.length = 0;
25+
}
26+
27+
public void push(int value){
28+
Node newNode = new Node(value);
29+
if(this.length == 0) {
30+
this.head = newNode;
31+
this.tail = newNode;
32+
} else {
33+
this.tail.next = newNode;
34+
newNode.prev = this.tail;
35+
this.tail = newNode;
36+
}
37+
this.length++;
38+
}
39+
40+
public boolean isPalindrome(){
41+
if(this.length < 2) return true;
42+
Node forwardNode = this.head;
43+
Node backwardNode = this.tail;
44+
45+
for (int i = 0; i < Math.floor(this.length/2); i++) {
46+
if(forwardNode.value != backwardNode.value) return false;
47+
forwardNode = forwardNode.next;
48+
backwardNode = backwardNode.prev;
49+
}
50+
51+
return true;
52+
}
53+
54+
public void printList() {
55+
Node temp = head;
56+
while(temp != null) {
57+
System.out.println(temp.value);
58+
temp = temp.next;
59+
}
60+
}
61+
}
62+
63+
public class PalindromeCheck {
64+
public static void main(String[] args) {
65+
DoublyLinkedlist myDoublyLinkedList1 = new DoublyLinkedlist();
66+
myDoublyLinkedList1.push(1);
67+
myDoublyLinkedList1.push(2);
68+
myDoublyLinkedList1.push(3);
69+
myDoublyLinkedList1.push(2);
70+
myDoublyLinkedList1.push(1);
71+
72+
DoublyLinkedlist myDoublyLinkedList2 = new DoublyLinkedlist();
73+
myDoublyLinkedList2.push(1);
74+
myDoublyLinkedList2.push(2);
75+
myDoublyLinkedList2.push(3);
76+
myDoublyLinkedList2.push(4);
77+
myDoublyLinkedList2.push(5);
78+
79+
System.out.println(myDoublyLinkedList1.isPalindrome());
80+
System.out.println(myDoublyLinkedList2.isPalindrome());
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
**Problem statement:**
2+
Given a doubly linked list. Implement `isPalindrome()` that verify the node values of a doubly linked list forms a palindrome or not.
3+
4+
## Examples:
5+
Example1:
6+
7+
Input: [1,2,3,2,1]
8+
Output: true
9+
10+
Example2:
11+
12+
Input: [1,2,3,4,5]
13+
Output: false
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of two pointers traversing from both ends of the list. The algorithmic approach can be summarized as follows:
17+
18+
1. Add an edge case returing true if the length of the list is less than 2.
19+
20+
2. Store the head and tail nodes in `forwardNode` and `backwardNode` respectively.
21+
22+
3. Iterate over the list until the index is less than middle element.
23+
24+
4. Compare both forward and backward nodes. Return false if they are not equal.
25+
26+
5. Update both forward and backward nodes to next and previous nodes respectively.
27+
28+
6. Return true after the loop indicating that palindrome exists with given nodes.
29+
30+
**Time and Space complexity:**
31+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are traversing through a list at most once.
32+
33+
Here, we don't use any additional datastructure other than few variables. Hence, the space complexity will be `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package java1.algorithms.doublyLinkedlist.swapFirstAndLast;
2+
3+
class Node {
4+
public int value;
5+
public Node next;
6+
public Node prev;
7+
8+
Node(int value){
9+
this.value = value;
10+
this.next = null;
11+
this.prev = null;
12+
}
13+
}
14+
15+
class DoublyLinkedlist {
16+
17+
private Node head;
18+
private Node tail;
19+
private int length;
20+
21+
DoublyLinkedlist(){
22+
this.head = null;
23+
this.tail = null;
24+
this.length = 0;
25+
}
26+
27+
public void push(int value){
28+
Node newNode = new Node(value);
29+
if(this.length == 0) {
30+
this.head = newNode;
31+
this.tail = newNode;
32+
} else {
33+
this.tail.next = newNode;
34+
newNode.prev = this.tail;
35+
this.tail = newNode;
36+
}
37+
this.length++;
38+
}
39+
40+
public void swapFirstAndLast(){
41+
if(this.length <= 1) return;
42+
int temp = this.head.value;
43+
this.head.value = this.tail.value;
44+
this.tail.value = temp;
45+
}
46+
47+
public void printList() {
48+
Node temp = head;
49+
while(temp != null) {
50+
System.out.println(temp.value);
51+
temp = temp.next;
52+
}
53+
}
54+
}
55+
56+
public class SwapFirstAndLast {
57+
58+
public static void main(String[] args) {
59+
DoublyLinkedlist myDoublyLinkedList = new DoublyLinkedlist();
60+
myDoublyLinkedList.push(1);
61+
myDoublyLinkedList.push(2);
62+
myDoublyLinkedList.push(3);
63+
myDoublyLinkedList.push(4);
64+
myDoublyLinkedList.push(5);
65+
66+
System.out.println("Before swap:");
67+
myDoublyLinkedList.printList();
68+
System.out.println("After swap:");
69+
myDoublyLinkedList.swapFirstAndLast();
70+
myDoublyLinkedList.printList();
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Problem statement:**
2+
Given a doubly linked list. Implement `swapFirstLast()` that swaps the values of the first and last nodes of doubly linked list.
3+
4+
## Examples:
5+
6+
Input: [1,2,3,4,5]
7+
Output: [5,2,3,4,1]
8+
9+
**Algorithmic Steps**
10+
This problem is solved by swapping the values of head and tail nodes. The algorithmic approach can be summarized as follows:
11+
12+
1. Add an edge case returing immediately if the length of the list is less than or equal to 1.
13+
14+
2. Store the head's value in a temporary variable `temp`.
15+
16+
3. Update the head's value with tail's value.
17+
18+
4. Modify the tail's value with a value stored in `temp` variable.
19+
20+
5. Now the list is modified with swapped values of first and last nodes.
21+
22+
**Time and Space complexity:**
23+
This algorithm has a time complexity of `O(1)`. This is because we are just swapping the first and last node values in a list.
24+
25+
Here, we don't use any additional datastructure other than few variables. Hence, the space complexity will be `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package java1.algorithms.doublyLinkedlist.swapNodePairs;
2+
3+
class Node {
4+
public int value;
5+
public Node next;
6+
public Node prev;
7+
8+
Node(int value){
9+
this.value = value;
10+
this.next = null;
11+
this.prev = null;
12+
}
13+
}
14+
15+
class DoublyLinkedlist {
16+
17+
private Node head;
18+
private Node tail;
19+
private int length;
20+
21+
DoublyLinkedlist(){
22+
this.head = null;
23+
this.tail = null;
24+
this.length = 0;
25+
}
26+
27+
public void push(int value){
28+
Node newNode = new Node(value);
29+
if(this.length == 0) {
30+
this.head = newNode;
31+
this.tail = newNode;
32+
} else {
33+
this.tail.next = newNode;
34+
newNode.prev = this.tail;
35+
this.tail = newNode;
36+
}
37+
this.length++;
38+
}
39+
40+
public void swapNodePairs(){
41+
if(this.length < 2) return;
42+
Node current = this.head;
43+
Node newHead = this.head.next;
44+
45+
while (current != null && current.next != null) {
46+
Node firstNext = current.next;
47+
Node secondNext = current.next.next;
48+
Node prev = current.prev;
49+
50+
if(prev != null) prev.next = firstNext;
51+
firstNext.next = current;
52+
current.next = secondNext;
53+
54+
if(secondNext != null) secondNext.prev = current;
55+
current.prev = firstNext;
56+
firstNext.prev = prev;
57+
58+
current = current.next;
59+
}
60+
61+
this.head = newHead;
62+
}
63+
64+
public void printList() {
65+
Node temp = head;
66+
while(temp != null) {
67+
System.out.println(temp.value);
68+
temp = temp.next;
69+
}
70+
}
71+
}
72+
73+
74+
public class SwapNodePairs {
75+
public static void main(String[] args) {
76+
DoublyLinkedlist myDoublyLinkedList1 = new DoublyLinkedlist();
77+
myDoublyLinkedList1.push(1);
78+
myDoublyLinkedList1.push(2);
79+
myDoublyLinkedList1.push(3);
80+
myDoublyLinkedList1.push(4);
81+
myDoublyLinkedList1.push(5);
82+
myDoublyLinkedList1.push(6);
83+
84+
System.out.println("Before swap node pairs:");
85+
myDoublyLinkedList1.printList();
86+
System.out.println("After swap node pairs:");
87+
myDoublyLinkedList1.swapNodePairs();
88+
myDoublyLinkedList1.printList();
89+
90+
DoublyLinkedlist myDoublyLinkedList2 = new DoublyLinkedlist();
91+
myDoublyLinkedList2.push(1);
92+
myDoublyLinkedList2.push(2);
93+
myDoublyLinkedList2.push(3);
94+
myDoublyLinkedList2.push(4);
95+
myDoublyLinkedList2.push(5);
96+
97+
System.out.println("Before swap node pairs:");
98+
myDoublyLinkedList2.printList();
99+
System.out.println("After swap node pairs:");
100+
myDoublyLinkedList2.swapNodePairs();
101+
myDoublyLinkedList2.printList();
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Problem statement:**
2+
Given a doubly linked list, swap every two adjacent nodes and return the modified list.
3+
4+
**Note:** The problem needs to be solved without modifying the values in the list's nodes (i.e., only the pointers needs to be updated).
5+
6+
## Examples:
7+
Example1:
8+
9+
Input: [1,2,3,4,5,6]
10+
Output: [2,1,4,3,6,5]
11+
12+
Example2:
13+
14+
Input: [1,2,3,4,5]
15+
Output: [2,1,4,3,5]]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of traversing the list and updating the pointers between nodes. The algorithmic approach can be summarized as follows:
19+
20+
1. Add an edge case returing immediately if the length of the list is less than 2.
21+
22+
2. Store the head and it's next nodes in `current` and `newHead` respectively.
23+
24+
3. Iterate over the list until the current and its next nodes are not equal to null.
25+
26+
4. Find the first next node `firstNext`(i.e,next to current node), second next node `secondNext`(current node's next next node) and previous node `prev`(previous node of current node) based on each iteration.
27+
28+
5. If the previous node is not equal to null, update the next pointer of previous node to firstNext. After that, the next pointer of firstNext to current node, and the next pointer of current to secondNext.
29+
30+
6. In the same way, if the secondNext is not equal to null, update the previous pointer of secondNext to current. After that, the previous pointer of current node to firstNext, and the previous pointer of firstNext to prev node.
31+
32+
7. As a last step, update the current node to its next node inorder to continue with traversal.
33+
34+
8. Finally, update the head node to newHead for the modified list.
35+
36+
**Time and Space complexity:**
37+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are traversing through a list at most once .
38+
39+
Here, we don't use any additional datastructure other than few variables. Hence, the space complexity will be `O(1)`.

0 commit comments

Comments
 (0)