Skip to content

Commit

Permalink
Create October-07.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunterdii authored Oct 7, 2024
1 parent 2dc1432 commit 83de0c9
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions October 2024 GFG SOLUTION/October-07.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine();
Solution soln = new Solution();
while (t-- > 0) {
Node head = null;
String input = scanner.nextLine();
Scanner ss = new Scanner(input);
List<Integer> arr = new ArrayList<>();
while (ss.hasNextInt()) {
arr.add(ss.nextInt());
}
int n = arr.size();
for (int i = 0; i < n; i++) {
int tmp = arr.get(i);
head = soln.insert(head, tmp);
}

ArrayList<Integer> list = soln.getList(head);
for (int x : list) System.out.print(x + " ");
System.out.println();

for (int i = list.size() - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
}
}

class Node {
int data;
Node npx;

Node(int x) {
data = x;
npx = null;
}
}

// } Driver Code Ends


// class Node {
// int data;
// Node npx;

// Node(int x) {
// data = x;
// npx = null;
// }
// }
class Solution {
static Node insert(Node head, int data) {
Node temp = new Node(data);
temp.npx = head;
head = temp;
return head;
}

static ArrayList<Integer> getList(Node head) {
ArrayList<Integer> ans = new ArrayList<>();
Node temp = head;
while (temp != null) {
ans.add(temp.data);
temp = temp.npx;
}
return ans;
}
}

0 comments on commit 83de0c9

Please sign in to comment.