Skip to content

Commit 65b4dcd

Browse files
committedJan 27, 2019
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 {

0 commit comments

Comments
 (0)