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
Lines changed: 59 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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 {

0 commit comments

Comments
 (0)