Skip to content

Added solution for reverse a linked list #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions Data-Structures/reverseLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;

public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}

class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;

public SinglyLinkedList() {
this.head = null;
this.tail = null;
}

public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);

if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}

this.tail = node;
}
}

class SinglyLinkedListPrintHelper {
public static void printList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
while (node != null) {
bufferedWriter.write(String.valueOf(node.data));

node = node.next;

if (node != null) {
bufferedWriter.write(sep);
}
}
}
}

class Result {

/*
* Complete the 'reverse' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/

/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/

public static SinglyLinkedListNode reverse(SinglyLinkedListNode llist) {
SinglyLinkedListNode prev = null;
SinglyLinkedListNode current = llist;
SinglyLinkedListNode next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
llist = prev;
return llist;

}

}

public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int tests = Integer.parseInt(bufferedReader.readLine().trim());

IntStream.range(0, tests).forEach(testsItr -> {
try {
SinglyLinkedList llist = new SinglyLinkedList();

int llistCount = Integer.parseInt(bufferedReader.readLine().trim());

IntStream.range(0, llistCount).forEach(i -> {
try {
int llistItem = Integer.parseInt(bufferedReader.readLine().trim());

llist.insertNode(llistItem);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

SinglyLinkedListNode llist1 = Result.reverse(llist.head);

SinglyLinkedListPrintHelper.printList(llist1, " ", bufferedWriter);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

bufferedReader.close();
bufferedWriter.close();
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ hackerrank solutions java GitHub | hackerrank tutorial in java | hackerrank 30 d
| Easy | [Insert a Node at the Tail of a Linked List](https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem)| [Solution3.java](./Data-Structures/insertAtEndOfLinkedList.java) | [YT Video](https://youtu.be/KVTaQ0jy7Jw)
| Easy | [Insert a node at a specific position in a linked list](https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem)| [Solution4.java](./Data-Structures/insertAtPositionOfLinkedList.java) | [YT Video](https://youtu.be/KVTaQ0jy7Jw)
| Easy | [Compare two linked lists](https://www.hackerrank.com/challenges/compare-two-linked-lists/problem)| [Solution5.java](./Data-Structures/compare-two-linked-lists.java) | [YT Video](https://youtu.be/KVTaQ0jy7Jw)
| Easy | [Reverse a linked list](https://www.hackerrank.com/challenges/reverse-a-linked-list/problem)| [Solution6.java](./Data-Structures/reverseLinkedList.java)

# Learning Resources
1.) [Cracking the Coding Interview](https://amzn.to/3fH727G)
Expand Down