Skip to content

Commit d6b8524

Browse files
committed
reverse dll and insert element in between
1 parent e3c05f3 commit d6b8524

File tree

11 files changed

+331
-16
lines changed

11 files changed

+331
-16
lines changed

Diff for: ShortCuts.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,22 @@ Custom Code:
114114
}
115115

116116
============================================
117+
## Find Tail or last element in linked list
117118
Out of the box:
119+
120+
LinkedList.getLast();
121+
122+
Custom Code:
123+
124+
// Iterate through list until last node's current is null
125+
while (current.next != null) {
126+
current = current.next;
127+
}
128+
118129

119130
Custom Code:
120131
============================================
121132
Out of the box:
122133

123-
Custom Code:
134+
Custom Code:
135+
============================================

Diff for: TODO.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
List of todo difficult or important questions:
2+
3+
### Reverse a doubly linked list
4+
https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem?filter=java8&filter_on=language&h_l=interview&page=1&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=linked-lists
5+
6+
7+
### Inserting a Node Into a Sorted Doubly Linked List
8+
https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=linked-lists
9+
1. Empty list
10+
2. At the beginning of the list
11+
3. Somewhere at the middle of the list
12+
4. At the end of the list
13+
14+

Diff for: src/codebase3/sorting/insertion/InsertionSort.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public class InsertionSort {
44

55
static void insertionSort(int[] A) {
66
for (int i = 1; i < A.length; i++) {
7-
int tmp = A[i], j = i;
7+
int j = i;
8+
int tmp = A[j];
89
while (j > 0 && A[j - 1] > tmp) {
910
A[j] = A[j - 1];
1011
j--;

Diff for: src/codebase3/sorting/quick/QuickSort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public static void quickSort(int[] array, int start, int end) {
1111

1212

1313
static int partition(int[] array, int p, int q) {
14-
int pivot = q;
14+
// int pivot = q;
1515
int i = p-1;
1616
for (int j = p; j <= q; j++) {
17-
if (array[j] <= array[pivot]) {
17+
if (array[j] <= array[q]) {
1818
i++;
1919
int temp = array[i];
2020
array[i] = array[j];

Diff for: src/codechallenge/linkedlist/ReverseDoublyLinkedlist.java

+71-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
1111
Challenge: Reverse a doubly linked list
1212
13+
* For your reference:
14+
*
15+
* MyDLLNode {
16+
* int data;
17+
* DoublyLinkedListNode next;
18+
* DoublyLinkedListNode prev;
19+
* }
20+
*
21+
22+
1323
*/
1424

1525

@@ -31,17 +41,26 @@ public void test() {
3141
n3.prev = n2;
3242
n2.prev = n1;
3343

34-
MyDLLNode newHead = reverseLL(n1);
44+
MyDLLNode newHead = reverse1(n1);
3545

3646
while(newHead.next != null){
3747
System.out.println(newHead.data + "> " + newHead.next.data);
3848
assert newHead.data > newHead.next.data;
3949
newHead = newHead.next;
4050
}
4151

52+
newHead = reverse2(n1);
53+
54+
while(newHead.next != null){
55+
System.out.println(newHead.data + "< " + newHead.next.data);
56+
assert newHead.data < newHead.next.data;
57+
newHead = newHead.next;
58+
}
59+
4260
}
4361

44-
private MyDLLNode reverseLL(MyDLLNode n1) {
62+
// solution 1
63+
private MyDLLNode reverse1(MyDLLNode n1) {
4564
List<MyDLLNode> al = new ArrayList<>();
4665
while(n1 != null){
4766
al.add(n1);
@@ -58,5 +77,55 @@ private MyDLLNode reverseLL(MyDLLNode n1) {
5877
}
5978

6079

80+
// solution 2
81+
private MyDLLNode reverse2(MyDLLNode head) {
82+
MyDLLNode prev = null, cur = head, next = null;
83+
while(cur != null) {
84+
next = cur.next;
85+
cur.next = prev;
86+
cur.prev = next;
87+
prev = cur;
88+
cur = next;
89+
}
90+
return prev;
91+
}
92+
93+
// solution 3 - incomplete
94+
private MyDLLNode reverse3(MyDLLNode head) {
95+
96+
MyDLLNode cn = head;
97+
MyDLLNode revHead = cn;
98+
while(cn != null){
99+
100+
System.out.println(cn.data);
101+
cn.prev = cn.next;
102+
cn = cn.next;
103+
// System.out.println("next" + head.data);
104+
}
105+
106+
cn = head;
107+
while (cn != null){
108+
revHead = cn;
109+
cn.next = cn.prev;
110+
System.out.println(cn.data);
111+
cn = cn.prev;
112+
}
113+
114+
cn = revHead;
115+
while (cn != null){
116+
// cn.next = cn.prev;
117+
System.out.println(cn.data);
118+
cn = cn.next;
119+
}
120+
121+
122+
// MyDLLNode newHead = head;
123+
// while(head.prev != null){
124+
// head.next = head.prev;
125+
// head = head.prev;
126+
// }
127+
return revHead;
128+
129+
}
61130
}
62131

Diff for: src/javaConcepts/lists/LinkedListDemo.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package javaConcepts.lists;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.util.LinkedList;
7+
8+
public class LinkedListDemo {
9+
10+
// Test Driven Development by Aseem Jain
11+
@Test
12+
public void test() {
13+
14+
LinkedList<String> ll = new LinkedList<>();
15+
ll.getLast();
16+
17+
}
18+
19+
20+
}

Diff for: src/me/premaseem/MyUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class MyUtils {
44

55
static public int[] getIntArr() {
6-
int[] arr = new int[]{70, 20, 30, 80, 40, 100};
6+
int[] arr = new int[]{70, -20, 30, 0, 40, 100};
77
System.out.print("Unsorted array looks like => ");
88
printArr(arr);
99
return arr;

Diff for: src/me/premaseem/practiceRepeatation/BubbleSort.java

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package me.premaseem.practiceRepeatation;
2+
3+
import me.premaseem.MyUtils;
4+
import org.junit.Test;
5+
6+
public class BubbleSort {
7+
// Test Driven Development by Aseem Jain
8+
@Test
9+
public void test() {
10+
11+
// Bubble Sort
12+
int[] intArr = MyUtils.getIntArr();
13+
bubbleSort(intArr);
14+
MyUtils.isArrSorted(intArr);
15+
16+
// Selection Sort
17+
intArr = MyUtils.getIntArr();
18+
selectionSort(intArr);
19+
MyUtils.isArrSorted(intArr);
20+
21+
// Insertion Sort
22+
intArr = MyUtils.getIntArr();
23+
insertionSort(intArr);
24+
MyUtils.isArrSorted(intArr);
25+
26+
// Quick Sort
27+
intArr = MyUtils.getIntArr();
28+
quicksort(intArr, 0, intArr.length - 1);
29+
MyUtils.isArrSorted(intArr);
30+
31+
// Merge Sort
32+
intArr = MyUtils.getIntArr();
33+
mergeSort(intArr, 0, intArr.length - 1);
34+
MyUtils.isArrSorted(intArr);
35+
36+
}
37+
38+
39+
40+
void mergeSort(int[] a, int l, int r){
41+
42+
if(r>l){
43+
int m = (l+r) / 2;
44+
mergeSort(a,l,m);
45+
mergeSort(a,m+1,r);
46+
merge(a,l,m,r);
47+
}
48+
49+
}
50+
51+
void merge(int[] a, int l, int m, int r){
52+
53+
int las = m-l+1;
54+
int ras = r-m;
55+
56+
// int create new array
57+
int[] la = new int[las];
58+
int[] ra = new int[ras];
59+
60+
// populate
61+
for (int i = l; i < las; i++) {
62+
la[i] = a[l+i];
63+
}
64+
65+
for (int i = m; i < ras; i++) {
66+
ra[i] = a[m+i];
67+
}
68+
69+
int li=0;
70+
int ri=0;
71+
for (int i = l; i <= r; i++) {
72+
73+
int lae = Integer.MAX_VALUE;
74+
int rae = Integer.MAX_VALUE;
75+
76+
if(li < las){
77+
lae = la[li];
78+
}
79+
80+
if(ri < ras){
81+
rae = ra[ri];
82+
}
83+
84+
if(lae < rae){
85+
a[i] = lae;
86+
li++;
87+
}else{
88+
a[i] = rae;
89+
ri++;
90+
}
91+
}
92+
93+
}
94+
95+
void bubbleSort(int[] a) {
96+
97+
for (int i = 0; i < a.length; i++) {
98+
for (int j = i; j < a.length - 1; j++) {
99+
if (a[j] > a[j + 1]) {
100+
int swap = a[j];
101+
a[j] = a[j + 1];
102+
a[j + 1] = swap;
103+
}
104+
}
105+
}
106+
}
107+
108+
void selectionSort(int[] a) {
109+
110+
for (int i = 0; i < a.length; i++) {
111+
int minI = i;
112+
for (int j = i; j < a.length; j++) {
113+
if(a[j] < a[minI] ){
114+
minI = j;
115+
}
116+
117+
}
118+
if(minI != i) {
119+
int t = a[minI];
120+
a[minI] = a[i];
121+
a[i] = t;
122+
}
123+
}
124+
125+
// for (int i = 0; i < a.length; i++) {
126+
// int minI = i;
127+
//
128+
// for (int j = i; j < a.length; j++) {
129+
// if (a[j] < a[minI]) {
130+
// minI = j;
131+
// }
132+
// }
133+
// if (minI != i) {
134+
// int swap = a[i];
135+
// a[i] = a[minI];
136+
// a[minI] = swap;
137+
// }
138+
// }
139+
}
140+
141+
void insertionSort(int[] a) {
142+
143+
for (int i = 1; i < a.length; i++) {
144+
int j = i;
145+
int insertionKey = a[j];
146+
147+
while (j > 0 && insertionKey < a[j - 1]) {
148+
a[j] = a[j - 1];
149+
j--;
150+
}
151+
// insertion key goes to the intented place.
152+
a[j] = insertionKey;
153+
154+
}
155+
}
156+
157+
void quicksort(int[] a, int s, int e) {
158+
if (s < e) {
159+
int pi = getPartition(a, s, e);
160+
quicksort(a, s, pi - 1);
161+
quicksort(a, pi + 1, e);
162+
163+
164+
}
165+
}
166+
167+
private int getPartition(int[] a, int s, int e) {
168+
int j = s - 1;
169+
for (int i = s; i <= e; i++) {
170+
if (a[i] <= a[e]) {
171+
j++;
172+
int swap = a[j];
173+
a[j] = a[i];
174+
a[i] = swap;
175+
}
176+
}
177+
return j;
178+
179+
180+
}
181+
182+
}

0 commit comments

Comments
 (0)