diff --git a/Data-Structures/reverseLinkedList.java b/Data-Structures/reverseLinkedList.java new file mode 100644 index 0000000..3fa370d --- /dev/null +++ b/Data-Structures/reverseLinkedList.java @@ -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(); + } +} diff --git a/README.md b/README.md index ace496e..df62d89 100644 --- a/README.md +++ b/README.md @@ -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)