diff --git a/Java/Data-Structures/LISTS/SINGLE/STANDARD/reversing_the_linked_list.java b/Java/Data-Structures/LISTS/SINGLE/STANDARD/reversing_the_linked_list.java new file mode 100644 index 0000000..8eb6eca --- /dev/null +++ b/Java/Data-Structures/LISTS/SINGLE/STANDARD/reversing_the_linked_list.java @@ -0,0 +1,104 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.regex.*; + +class Solution { + + static class SinglyLinkedListNode { + public int data; + public SinglyLinkedListNode next; + + public SinglyLinkedListNode(int nodeData) { + this.data = nodeData; + this.next = null; + } + } + + static 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; + } + } + + public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep) { + while (node != null) { + System.out.print(node.data); + + node = node.next; + + if (node != null) { + System.out.print(sep); + } + } + } + + + static void reversePrint(SinglyLinkedListNode head) { + SinglyLinkedListNode current=head; + SinglyLinkedListNode prev=null; + SinglyLinkedListNode next=null; + + + while(current!=null) + { + next=current.next; + current.next=prev; + prev=current; + current=next; + } + head=prev; + SinglyLinkedListNode n=head; + while(n.next!=null) + { + System.out.println(n.data); + n=n.next; + } + System.out.println(n.data); + + } + + private static final Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) { + int tests = scanner.nextInt(); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + for (int testsItr = 0; testsItr < tests; testsItr++) { + SinglyLinkedList llist = new SinglyLinkedList(); + + int llistCount = scanner.nextInt(); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + for (int i = 0; i < llistCount; i++) { + int llistItem = scanner.nextInt(); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + llist.insertNode(llistItem); + } + + reversePrint(llist.head); + } + + scanner.close(); + } +}