└── reverse linked list.txt /reverse linked list.txt: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.function.*; 8 | import java.util.regex.*; 9 | import java.util.stream.*; 10 | import static java.util.stream.Collectors.joining; 11 | import static java.util.stream.Collectors.toList; 12 | 13 | class SinglyLinkedListNode { 14 | public int data; 15 | public SinglyLinkedListNode next; 16 | 17 | public SinglyLinkedListNode(int nodeData) { 18 | this.data = nodeData; 19 | this.next = null; 20 | } 21 | } 22 | 23 | class SinglyLinkedList { 24 | public SinglyLinkedListNode head; 25 | public SinglyLinkedListNode tail; 26 | 27 | public SinglyLinkedList() { 28 | this.head = null; 29 | this.tail = null; 30 | } 31 | 32 | public void insertNode(int nodeData) { 33 | SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData); 34 | 35 | if (this.head == null) { 36 | this.head = node; 37 | } else { 38 | this.tail.next = node; 39 | } 40 | 41 | this.tail = node; 42 | } 43 | } 44 | 45 | class SinglyLinkedListPrintHelper { 46 | public static void printList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException { 47 | while (node != null) { 48 | bufferedWriter.write(String.valueOf(node.data)); 49 | 50 | node = node.next; 51 | 52 | if (node != null) { 53 | bufferedWriter.write(sep); 54 | } 55 | } 56 | } 57 | } 58 | 59 | class Result { 60 | 61 | /* 62 | * Complete the 'reverse' function below. 63 | * 64 | * The function is expected to return an INTEGER_SINGLY_LINKED_LIST. 65 | * The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter. 66 | */ 67 | 68 | /* 69 | * For your reference: 70 | * 71 | * SinglyLinkedListNode { 72 | * int data; 73 | * SinglyLinkedListNode next; 74 | * } 75 | * 76 | */ 77 | 78 | public static SinglyLinkedListNode reverse(SinglyLinkedListNode llist) { 79 | // Write your code here 80 | SinglyLinkedListNode prev = null; 81 | SinglyLinkedListNode current = llist; 82 | SinglyLinkedListNode next = null; 83 | 84 | 85 | while (current != null) { 86 | next = current.next; 87 | current.next = prev; 88 | prev = current; 89 | current = next; 90 | } 91 | 92 | 93 | return prev; 94 | } 95 | 96 | 97 | } 98 | 99 | public class Solution { 100 | public static void main(String[] args) throws IOException { 101 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 102 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 103 | 104 | int tests = Integer.parseInt(bufferedReader.readLine().trim()); 105 | 106 | IntStream.range(0, tests).forEach(testsItr -> { 107 | try { 108 | SinglyLinkedList llist = new SinglyLinkedList(); 109 | 110 | int llistCount = Integer.parseInt(bufferedReader.readLine().trim()); 111 | 112 | IntStream.range(0, llistCount).forEach(i -> { 113 | try { 114 | int llistItem = Integer.parseInt(bufferedReader.readLine().trim()); 115 | 116 | llist.insertNode(llistItem); 117 | } catch (IOException ex) { 118 | throw new RuntimeException(ex); 119 | } 120 | }); 121 | 122 | SinglyLinkedListNode llist1 = Result.reverse(llist.head); 123 | 124 | SinglyLinkedListPrintHelper.printList(llist1, " ", bufferedWriter); 125 | bufferedWriter.newLine(); 126 | } catch (IOException ex) { 127 | throw new RuntimeException(ex); 128 | } 129 | }); 130 | 131 | bufferedReader.close(); 132 | bufferedWriter.close(); 133 | } 134 | } 135 | --------------------------------------------------------------------------------