Skip to content

Commit 65b4dcd

Browse files
committed
Some minor refactorings
1 parent ea35359 commit 65b4dcd

File tree

194 files changed

+450
-392
lines changed

Some content is hidden

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

194 files changed

+450
-392
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
11
package com.ctci.linkedlists;
22

3+
import static com.ctci.linkedlists.Node.printList;
4+
35
/**
46
* @author rampatra
57
* @since 2019-01-27
68
*/
79
public class DeleteMiddleNode {
10+
11+
/**
12+
* Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not
13+
* necessarily the exact middle) of a singly linked list, given only access to that node.
14+
* <p>
15+
* EXAMPLE
16+
* Input: the node c from the linked list a->b->c->d->e->f
17+
* Result: nothing is returned, but the new linked list looks like a->b->d->e->f
18+
*
19+
* @param middle the node to be deleted
20+
*/
21+
private static void deleteMiddleNode(Node middle) {
22+
if (middle == null || middle.next == null) {
23+
return;
24+
}
25+
// copy the data from the next node over to the middle node, and then to delete the next node
26+
Node next = middle.next;
27+
middle.val = next.val;
28+
middle.next = next.next;
29+
}
30+
31+
public static void main(String[] args) {
32+
Node l1 = new Node(1);
33+
l1.next = new Node(2);
34+
l1.next.next = new Node(3);
35+
l1.next.next.next = new Node(4);
36+
l1.next.next.next.next = new Node(5);
37+
l1.next.next.next.next.next = new Node(6);
38+
printList(l1);
39+
deleteMiddleNode(l1.next.next);
40+
printList(l1);
41+
42+
System.out.println("----");
43+
44+
l1 = new Node(1);
45+
l1.next = new Node(2);
46+
l1.next.next = new Node(3);
47+
printList(l1);
48+
deleteMiddleNode(l1.next);
49+
printList(l1);
50+
51+
System.out.println("----");
52+
53+
l1 = new Node(1);
54+
l1.next = new Node(3);
55+
printList(l1);
56+
deleteMiddleNode(l1);
57+
printList(l1);
58+
59+
System.out.println("----");
60+
61+
l1 = new Node(1);
62+
l1.next = new Node(3);
63+
printList(l1);
64+
deleteMiddleNode(l1.next);
65+
printList(l1);
66+
}
867
}

src/main/java/com/hackerrank/algorithms/strings/AlternatingCharacters.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 10/22/15
8+
* @author rampatra
9+
* @since 10/22/15
1010
* @time: 9:24 AM
1111
*/
1212
public class AlternatingCharacters {

src/main/java/com/hackerrank/algorithms/strings/Pangram.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 10/22/15
8+
* @author rampatra
9+
* @since 10/22/15
1010
* @time: 8:47 AM
1111
*/
1212
public class Pangram {

src/main/java/com/hackerrank/algorithms/strings/TwoStrings.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 10/22/15
8+
* @author rampatra
9+
* @since 10/22/15
1010
* @time: 10:08 AM
1111
*/
1212
public class TwoStrings {

src/main/java/com/hackerrank/bitmanipulation/CounterGame.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Created by IntelliJ IDEA.
88
*
9-
* @author: ramswaroop
10-
* @date: 6/24/15
9+
* @author rampatra
10+
* @since 6/24/15
1111
* @time: 12:03 PM
1212
*/
1313
public class CounterGame {

src/main/java/com/hackerrank/bitmanipulation/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
/**
88
* Created by IntelliJ IDEA.
99
*
10-
* @author: ramswaroop
11-
* @date: 6/24/15
10+
* @author rampatra
11+
* @since 6/24/15
1212
* @time: 10:25 PM
1313
*/
1414
public class Solution {

src/main/java/com/hackerrank/bitmanipulation/TwosCompliment.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 6/24/15
8+
* @author rampatra
9+
* @since 6/24/15
1010
* @time: 10:25 PM
1111
*/
1212
public class TwosCompliment {

src/main/java/com/hackerrank/java/oops/JavaInheritance.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 7/19/15
6+
* @author rampatra
7+
* @since 7/19/15
88
* @time: 3:36 PM
99
*/
1010
public class JavaInheritance {

src/main/java/com/hackerrank/projecteuler/MultiplesOf3and5.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 1/1/16
8+
* @author rampatra
9+
* @since 1/1/16
1010
* @time: 8:48 AM
1111
*/
1212
public class MultiplesOf3and5 {

src/main/java/com/rampatra/arrays/ArrangeNosToFormBiggestNo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Created by IntelliJ IDEA.
88
*
9-
* @author: ramswaroop
10-
* @date: 11/1/15
9+
* @author rampatra
10+
* @since 11/1/15
1111
* @time: 8:53 PM
1212
*/
1313
public class ArrangeNosToFormBiggestNo {

src/main/java/com/rampatra/arrays/BooleanMatrix.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 9/11/15
6+
* @author rampatra
7+
* @since 9/11/15
88
* @time: 3:28 PM
99
* @see: http://www.geeksforgeeks.org/a-boolean-matrix-question/
1010
*/

src/main/java/com/rampatra/arrays/CelebrityProblem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Created by IntelliJ IDEA.
88
*
9-
* @author: ramswaroop
10-
* @date: 10/2/15
9+
* @author rampatra
10+
* @since 10/2/15
1111
* @time: 10:48 PM
1212
* <p/>
1313
* In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes,

src/main/java/com/rampatra/arrays/ConsecutiveElements.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 8/28/15
6+
* @author rampatra
7+
* @since 8/28/15
88
* @time: 10:32 AM
99
*/
1010
public class ConsecutiveElements {

src/main/java/com/rampatra/arrays/CountSmallerElementsOnRHS.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 9/19/15
8+
* @author rampatra
9+
* @since 9/19/15
1010
* @time: 11:33 PM
1111
*/
1212
public class CountSmallerElementsOnRHS {

src/main/java/com/rampatra/arrays/DuplicatesInArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 8/21/15
8+
* @author rampatra
9+
* @since 8/21/15
1010
* @time: 9:41 AM
1111
*/
1212
public class DuplicatesInArray {

src/main/java/com/rampatra/arrays/DuplicatesInArrayWithinKDistance.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Created by IntelliJ IDEA.
88
*
9-
* @author: ramswaroop
10-
* @date: 10/18/15
9+
* @author rampatra
10+
* @since 10/18/15
1111
* @time: 8:40 PM
1212
*/
1313
public class DuplicatesInArrayWithinKDistance {

src/main/java/com/rampatra/arrays/EqualProbabilityRandomNoGenerator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* which uses f(n) to generate non repeating random numbers from
1010
* {@code 0} (inclusive) to {@code n} (exclusive)?
1111
*
12-
* @author: ramswaroop
13-
* @date: 8/13/15
12+
* @author rampatra
13+
* @since 8/13/15
1414
* @time: 1:41 PM
1515
*/
1616
public class EqualProbabilityRandomNoGenerator {

src/main/java/com/rampatra/arrays/EquilibriumIndex.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 8/22/15
6+
* @author rampatra
7+
* @since 8/22/15
88
* @time: 11:56 AM
99
*/
1010
public class EquilibriumIndex {

src/main/java/com/rampatra/arrays/FixedPoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 9/8/15
6+
* @author rampatra
7+
* @since 9/8/15
88
* @time: 11:34 PM
99
*/
1010
public class FixedPoint {

src/main/java/com/rampatra/arrays/IntersectionAndUnionOf2SortedArrays.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 8/13/15
8+
* @author rampatra
9+
* @since 8/13/15
1010
* @time: 3:56 PM
1111
*/
1212
public class IntersectionAndUnionOf2SortedArrays {

src/main/java/com/rampatra/arrays/InversionsInArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 7/29/15
8+
* @author rampatra
9+
* @since 7/29/15
1010
* @time: 8:37 PM
1111
*/
1212
public class InversionsInArray {

src/main/java/com/rampatra/arrays/KLargestElements.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
/**
88
* Created by IntelliJ IDEA.
99
*
10-
* @author: ramswaroop
11-
* @date: 8/3/15
10+
* @author rampatra
11+
* @since 8/3/15
1212
* @time: 3:47 PM
1313
*/
1414
public class KLargestElements {

src/main/java/com/rampatra/arrays/KthLargestElement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
/**
99
* Created by IntelliJ IDEA.
1010
*
11-
* @author: ramswaroop
12-
* @date: 8/1/15
11+
* @author rampatra
12+
* @since 8/1/15
1313
* @time: 11:26 PM
1414
*/
1515
public class KthLargestElement {

src/main/java/com/rampatra/arrays/LargestProductContiguousSubArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 5/28/15
6+
* @author rampatra
7+
* @since 5/28/15
88
* @time: 12:44 PM
99
*/
1010
public class LargestProductContiguousSubArray {

src/main/java/com/rampatra/arrays/LargestSumContiguousSubArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 5/28/15
6+
* @author rampatra
7+
* @since 5/28/15
88
* @time: 12:44 PM
99
*/
1010
public class LargestSumContiguousSubArray {

src/main/java/com/rampatra/arrays/LeadersInArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 7/29/15
8+
* @author rampatra
9+
* @since 7/29/15
1010
* @time: 12:06 PM
1111
*/
1212
public class LeadersInArray {

src/main/java/com/rampatra/arrays/LongestBitonicSubArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 9/18/15
6+
* @author rampatra
7+
* @since 9/18/15
88
* @time: 5:10 PM
99
*/
1010
public class LongestBitonicSubArray {

src/main/java/com/rampatra/arrays/MajorityElement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 5/20/15
6+
* @author rampatra
7+
* @since 5/20/15
88
* @time: 2:36 PM
99
*/
1010

src/main/java/com/rampatra/arrays/MajorityElementInSortedArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 7/31/15
6+
* @author rampatra
7+
* @since 7/31/15
88
* @time: 10:02 AM
99
*/
1010
public class MajorityElementInSortedArray {

src/main/java/com/rampatra/arrays/MatrixInSpiral.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Created by IntelliJ IDEA.
77
*
8-
* @author: ramswaroop
9-
* @date: 9/9/15
8+
* @author rampatra
9+
* @since 9/9/15
1010
* @time: 2:55 PM
1111
*/
1212
public class MatrixInSpiral {

src/main/java/com/rampatra/arrays/MaxDiffWithLargerElementAfterSmallerElement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Created by IntelliJ IDEA.
55
*
6-
* @author: ramswaroop
7-
* @date: 8/12/15
6+
* @author rampatra
7+
* @since 8/12/15
88
* @time: 5:40 PM
99
*/
1010
public class MaxDiffWithLargerElementAfterSmallerElement {

0 commit comments

Comments
 (0)