From a3b7d0b5588be3144770f284f0a3815f74d3dd48 Mon Sep 17 00:00:00 2001 From: aitorfi Date: Wed, 27 Oct 2021 19:08:02 +0200 Subject: [PATCH 1/5] Fixes #2360 Palindrome Linked List --- Misc/PalindromeSinglyLinkedList.java | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Misc/PalindromeSinglyLinkedList.java diff --git a/Misc/PalindromeSinglyLinkedList.java b/Misc/PalindromeSinglyLinkedList.java new file mode 100644 index 000000000000..830d61becb38 --- /dev/null +++ b/Misc/PalindromeSinglyLinkedList.java @@ -0,0 +1,59 @@ +package Misc; + +import java.util.Stack; + +/** + * A simple way of knowing if a singly linked list is palindrome is to + * push all the values into a Stack and then compare the list to popped + * vales from the Stack. + * + * See more: https://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ + */ +public class PalindromeSinglyLinkedList { + public static void main(String[] args) { + Node node5 = new Node(3, null); + Node node4 = new Node(2, node5); + Node node3 = new Node(1, node4); + Node node2 = new Node(2, node3); + Node node1 = new Node(3, node2); + // Node node1 = new Node(2, node2); + + if (isPalindrome(node1)) { + System.out.println("It's a palindrome list"); + } else { + System.out.println("It's NOT a palindrome list"); + } + } + + public static boolean isPalindrome(Node head) { + boolean ret = true; + Node tempNode = head; + Stack linkedListValues = new Stack<>(); + + while (tempNode != null) { + linkedListValues.push(tempNode.data); + tempNode = tempNode.nextNode; + } + + while (head != null) { + if (head.data != linkedListValues.pop()) { + ret = false; + break; + } + + head = head.nextNode; + } + + return ret; + } +} + +class Node { + int data; + Node nextNode; + + public Node(int data, Node nextNode) { + this.data = data; + this.nextNode = nextNode; + } +} From fd4aea9f605f3219e466af7701f6ede8f21a3da9 Mon Sep 17 00:00:00 2001 From: aitorfi Date: Wed, 27 Oct 2021 19:37:21 +0200 Subject: [PATCH 2/5] Add ReverseNumber --- Maths/ReverseNumber.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Maths/ReverseNumber.java diff --git a/Maths/ReverseNumber.java b/Maths/ReverseNumber.java new file mode 100644 index 000000000000..6f0ffa7ca3d6 --- /dev/null +++ b/Maths/ReverseNumber.java @@ -0,0 +1,30 @@ +package Maths; + +import java.util.Scanner; +import java.util.NoSuchElementException; +import java.lang.IllegalStateException; + +public class ReverseNumber { + public static void main(String[] args) { + int number; + int reverse = 0; + + try (Scanner sc = new Scanner(System.in)) { + System.out.println("Enter a number:"); + number = sc.nextInt(); + } catch (NoSuchElementException | IllegalStateException e) { + System.out.println("ERROR: Invalid input"); + return; + } + + + while(number != 0) { + int remainder = number % 10; + + reverse = reverse * 10 + remainder; + number = number/10; + } + + System.out.println("The reverse of the given number is: " + reverse); + } +} From 88d281cb9cde78730aace38fefc31e1fbe673add Mon Sep 17 00:00:00 2001 From: aitorfi Date: Wed, 27 Oct 2021 19:44:18 +0200 Subject: [PATCH 3/5] Revert "Add ReverseNumber" This reverts commit fd4aea9f605f3219e466af7701f6ede8f21a3da9. --- Maths/ReverseNumber.java | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 Maths/ReverseNumber.java diff --git a/Maths/ReverseNumber.java b/Maths/ReverseNumber.java deleted file mode 100644 index 6f0ffa7ca3d6..000000000000 --- a/Maths/ReverseNumber.java +++ /dev/null @@ -1,30 +0,0 @@ -package Maths; - -import java.util.Scanner; -import java.util.NoSuchElementException; -import java.lang.IllegalStateException; - -public class ReverseNumber { - public static void main(String[] args) { - int number; - int reverse = 0; - - try (Scanner sc = new Scanner(System.in)) { - System.out.println("Enter a number:"); - number = sc.nextInt(); - } catch (NoSuchElementException | IllegalStateException e) { - System.out.println("ERROR: Invalid input"); - return; - } - - - while(number != 0) { - int remainder = number % 10; - - reverse = reverse * 10 + remainder; - number = number/10; - } - - System.out.println("The reverse of the given number is: " + reverse); - } -} From 213c58223918f1ad8aa2d5a2260ef111b14d58ee Mon Sep 17 00:00:00 2001 From: aitorfi Date: Wed, 27 Oct 2021 19:48:49 +0200 Subject: [PATCH 4/5] Add ReverseNumber --- Maths/ReverseNumber.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Maths/ReverseNumber.java diff --git a/Maths/ReverseNumber.java b/Maths/ReverseNumber.java new file mode 100644 index 000000000000..6f0ffa7ca3d6 --- /dev/null +++ b/Maths/ReverseNumber.java @@ -0,0 +1,30 @@ +package Maths; + +import java.util.Scanner; +import java.util.NoSuchElementException; +import java.lang.IllegalStateException; + +public class ReverseNumber { + public static void main(String[] args) { + int number; + int reverse = 0; + + try (Scanner sc = new Scanner(System.in)) { + System.out.println("Enter a number:"); + number = sc.nextInt(); + } catch (NoSuchElementException | IllegalStateException e) { + System.out.println("ERROR: Invalid input"); + return; + } + + + while(number != 0) { + int remainder = number % 10; + + reverse = reverse * 10 + remainder; + number = number/10; + } + + System.out.println("The reverse of the given number is: " + reverse); + } +} From bc2391defacb6dccf17f8f2ab980f502ca661a64 Mon Sep 17 00:00:00 2001 From: aitorfi Date: Fri, 29 Oct 2021 16:13:38 +0200 Subject: [PATCH 5/5] Revert "Fixes #2360 Palindrome Linked List" This reverts commit a3b7d0b5588be3144770f284f0a3815f74d3dd48. --- Misc/PalindromeSinglyLinkedList.java | 59 ---------------------------- 1 file changed, 59 deletions(-) delete mode 100644 Misc/PalindromeSinglyLinkedList.java diff --git a/Misc/PalindromeSinglyLinkedList.java b/Misc/PalindromeSinglyLinkedList.java deleted file mode 100644 index 830d61becb38..000000000000 --- a/Misc/PalindromeSinglyLinkedList.java +++ /dev/null @@ -1,59 +0,0 @@ -package Misc; - -import java.util.Stack; - -/** - * A simple way of knowing if a singly linked list is palindrome is to - * push all the values into a Stack and then compare the list to popped - * vales from the Stack. - * - * See more: https://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ - */ -public class PalindromeSinglyLinkedList { - public static void main(String[] args) { - Node node5 = new Node(3, null); - Node node4 = new Node(2, node5); - Node node3 = new Node(1, node4); - Node node2 = new Node(2, node3); - Node node1 = new Node(3, node2); - // Node node1 = new Node(2, node2); - - if (isPalindrome(node1)) { - System.out.println("It's a palindrome list"); - } else { - System.out.println("It's NOT a palindrome list"); - } - } - - public static boolean isPalindrome(Node head) { - boolean ret = true; - Node tempNode = head; - Stack linkedListValues = new Stack<>(); - - while (tempNode != null) { - linkedListValues.push(tempNode.data); - tempNode = tempNode.nextNode; - } - - while (head != null) { - if (head.data != linkedListValues.pop()) { - ret = false; - break; - } - - head = head.nextNode; - } - - return ret; - } -} - -class Node { - int data; - Node nextNode; - - public Node(int data, Node nextNode) { - this.data = data; - this.nextNode = nextNode; - } -}