From f2ac00f9a5c6a72af410537022511705a1623a6c Mon Sep 17 00:00:00 2001 From: arnauvila33 Date: Tue, 6 Oct 2020 01:11:58 -0400 Subject: [PATCH] Made ReversedLinkedList in JAVA for hacktoberfest. --- .classpath | 9 +++ .gitignore | 1 + .project | 17 ++++++ Java/ReversedLinkedList__arnauvila33.java | 71 +++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 Java/ReversedLinkedList__arnauvila33.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..814bfca --- /dev/null +++ b/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..110793b --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Snippets + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Java/ReversedLinkedList__arnauvila33.java b/Java/ReversedLinkedList__arnauvila33.java new file mode 100644 index 0000000..bc2f08d --- /dev/null +++ b/Java/ReversedLinkedList__arnauvila33.java @@ -0,0 +1,71 @@ + + +public class ReversedLinkedList__arnauvila33 { + + static Node head; + + static class Node{ + int data; + public Node next; + Node(int data){ + this.data=data; + next=null; + } + } + + /** + * Recursive method to reverse a Linked List. + * @param node The head node of the linked list. + * @return the inversed linked list. + */ + private static Node reverseList(Node node) { + if (node == null || node.next == null) + return node; + + + Node rest = reverseList(node.next); + node.next.next = node; + + + node.next = null; + + + return rest; + } + + /** + * Method to print the list passed. + * @param head the head of the list to print. + */ + private static void print(Node head) { + Node temp = head; + while (temp != null) { + System.out.print(temp.data + " "); + temp = temp.next; + } + System.out.println(); + } + + /** + * Main method to test the reversedList method. + * @param args argument passed. + */ + public static void main(String[] args) { + ReversedLinkedList__arnauvila33 list=new ReversedLinkedList__arnauvila33(); + list.head=new Node(15); + list.head.next = new Node(50); + list.head.next.next = new Node(100); + list.head.next.next.next = new Node(300); + + System.out.print("Linked List: "); + list.print(list.head); + + //Reversing the list + list.head = list.reverseList(list.head); + + System.out.println(""); + System.out.print("Linked List Reversed: "); + list.print(list.head); + + } +}