From a18836a04c9baec09af5c13c031836a4227ea5dd Mon Sep 17 00:00:00 2001 From: Manish Tambe <52567320+manishtambe@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:53:15 +0530 Subject: [PATCH 1/4] Add files via upload Added Cycle Detection In Linked List Problem & Palindrome Checker Problem In The Miscellaneous ! --- Miscellaneous/DetectCycleInList.java | 128 +++++++++++++++++++++++++++ Miscellaneous/PalindromeList.java | 108 ++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 Miscellaneous/DetectCycleInList.java create mode 100644 Miscellaneous/PalindromeList.java diff --git a/Miscellaneous/DetectCycleInList.java b/Miscellaneous/DetectCycleInList.java new file mode 100644 index 0000000..66de734 --- /dev/null +++ b/Miscellaneous/DetectCycleInList.java @@ -0,0 +1,128 @@ +// Problem Statement: - +// Given head, the head of a linked list, determine if the linked list has a cycle in it. +// There is a cycle in a linked list if there is some node in the list that can be reached +// again by continuously following the next pointer. Internally, pos is used to denote the +// index of the node that tail's next pointer is connected to. Note that pos is not passed +// as a parameter. +// Return true if there is a cycle in the linked list. Otherwise, return false. + +// Example 1: +// Sample Input : +// head = [3,2,0,-4], pos = 1 +// Output: true +// Explanation: There is a cycle in the linked list, where the tail connects to +// the 1st node (0-indexed). + +import java.util.*; + +class Node{ + int Data; + Node next; +}; + +public class DetectCycleInList { + public Node InsertNode(Node Head, int iNo) + { + Node newn = new Node(); + newn.Data = iNo; + newn.next = null; + + if(Head == null) + { + Head = newn; + } + else + { + Node temp = Head; + while(temp.next != null) + { + temp = temp.next; + } + temp.next = newn; + newn.next = null; + } + return Head; + } + + public void DisplayList(Node Head) + { + Node temp = Head; + + while(temp != null) + { + System.out.print("|"+temp.Data+"| ->"); + temp = temp.next; + } + System.out.print(" NULL\n"); + } + + public void CreateCycle(Node Head) + { + Node temp = Head; + Node temp1 = Head.next.next; + temp.next.next.next.next = temp1; + + } + + public Node DetectCycle(Node Head) + { + HashMap address = new HashMap<>(); + Node temp = Head; + int i = 0; + while(temp != null) + { + if(address.containsKey(temp)) + { + return temp; + } + else + { + address.put(temp, i); + i++; + } + temp = temp.next; + } + return temp; + } + + public boolean Result(Node Head) + { + boolean flag = false; + Node temp = DetectCycle(Head); + if(temp == null) + { + flag = false; + } + else + { + flag = true; + } + + return flag; + } + + public static void main(String args[]) + { + Node Head = null; + Node temp = null; + DetectCycleInList cobj = new DetectCycleInList(); + + Head = cobj.InsertNode(Head, 3); + Head = cobj.InsertNode(Head, 2); + Head = cobj.InsertNode(Head, 0); + Head = cobj.InsertNode(Head, -4); + + cobj.DisplayList(Head); + cobj.CreateCycle(Head); + //cobj.DisplayList(Head); + temp = cobj.DetectCycle(Head); + if(temp == null) + { + System.out.println("Cycle Is Not Present In The Given Linked List !"); + } + else + { + System.out.println("Cycle Is Present In The Given Linked List !"); + } + } +} diff --git a/Miscellaneous/PalindromeList.java b/Miscellaneous/PalindromeList.java new file mode 100644 index 0000000..2b26a15 --- /dev/null +++ b/Miscellaneous/PalindromeList.java @@ -0,0 +1,108 @@ +// Given the head of a singly linked list, return true if it is a palindrome or false otherwise. + +// Example 1: +// Sample Input: +// Input: head = [1,2,2,1] +// Output: Output: true + +import java.util.*; + +class Node{ + int Data; + Node next; +}; + +public class PalindromeList { + public Node InsertNode(Node Head, int iNo) + { + Node newn = new Node(); + newn.Data = iNo; + newn.next = null; + + if(Head == null) + { + Head = newn; + } + else + { + Node temp = Head; + while(temp.next != null) + { + temp = temp.next; + } + temp.next = newn; + newn.next = null; + } + return Head; + } + + public boolean CompareEle(Node Head, Stack snode) + { + boolean bflag = true; + Node temp = Head; + int iData1; + int iData2; + + while(temp != null) + { + iData1 = snode.pop(); + iData2 = temp.Data; + if(iData1 != iData2) + { + bflag = false; + } + temp = temp.next; + } + return bflag; + } + + public boolean isPalindrome(Node head) { + Node temp; + temp = head; + boolean iRet = false; + Stack snode = new Stack(); + + while(temp != null) + { + //System.out.print("|" + temp.data + "| ->\t"); + snode.add(temp.Data); + temp = temp.next; + } + + iRet = CompareEle(head, snode); + + if(iRet == true) + { + iRet = true; + } + else + { + iRet = false; + } + + return iRet; + } + + public static void main(String args[]) { + Node Head = null; + PalindromeList pobj = new PalindromeList(); + boolean iRet = false; + + Head = pobj.InsertNode(Head, 1); + Head = pobj.InsertNode(Head, 2); + Head = pobj.InsertNode(Head, 2); + Head = pobj.InsertNode(Head, 1); + + iRet = pobj.isPalindrome(Head); + + if(iRet == true) + { + System.out.println("Entered Linked List Is Palindrome !"); + } + else + { + System.out.println("Entered Linked List Is Not Palindrome !"); + } + + } +} From f010a0f88e958ab9aa729a6e119f74fba071dc88 Mon Sep 17 00:00:00 2001 From: Manish Tambe <52567320+manishtambe@users.noreply.github.com> Date: Wed, 5 Oct 2022 15:07:27 +0530 Subject: [PATCH 2/4] Delete DetectCycleInList.java --- Miscellaneous/DetectCycleInList.java | 128 --------------------------- 1 file changed, 128 deletions(-) delete mode 100644 Miscellaneous/DetectCycleInList.java diff --git a/Miscellaneous/DetectCycleInList.java b/Miscellaneous/DetectCycleInList.java deleted file mode 100644 index 66de734..0000000 --- a/Miscellaneous/DetectCycleInList.java +++ /dev/null @@ -1,128 +0,0 @@ -// Problem Statement: - -// Given head, the head of a linked list, determine if the linked list has a cycle in it. -// There is a cycle in a linked list if there is some node in the list that can be reached -// again by continuously following the next pointer. Internally, pos is used to denote the -// index of the node that tail's next pointer is connected to. Note that pos is not passed -// as a parameter. -// Return true if there is a cycle in the linked list. Otherwise, return false. - -// Example 1: -// Sample Input : -// head = [3,2,0,-4], pos = 1 -// Output: true -// Explanation: There is a cycle in the linked list, where the tail connects to -// the 1st node (0-indexed). - -import java.util.*; - -class Node{ - int Data; - Node next; -}; - -public class DetectCycleInList { - public Node InsertNode(Node Head, int iNo) - { - Node newn = new Node(); - newn.Data = iNo; - newn.next = null; - - if(Head == null) - { - Head = newn; - } - else - { - Node temp = Head; - while(temp.next != null) - { - temp = temp.next; - } - temp.next = newn; - newn.next = null; - } - return Head; - } - - public void DisplayList(Node Head) - { - Node temp = Head; - - while(temp != null) - { - System.out.print("|"+temp.Data+"| ->"); - temp = temp.next; - } - System.out.print(" NULL\n"); - } - - public void CreateCycle(Node Head) - { - Node temp = Head; - Node temp1 = Head.next.next; - temp.next.next.next.next = temp1; - - } - - public Node DetectCycle(Node Head) - { - HashMap address = new HashMap<>(); - Node temp = Head; - int i = 0; - while(temp != null) - { - if(address.containsKey(temp)) - { - return temp; - } - else - { - address.put(temp, i); - i++; - } - temp = temp.next; - } - return temp; - } - - public boolean Result(Node Head) - { - boolean flag = false; - Node temp = DetectCycle(Head); - if(temp == null) - { - flag = false; - } - else - { - flag = true; - } - - return flag; - } - - public static void main(String args[]) - { - Node Head = null; - Node temp = null; - DetectCycleInList cobj = new DetectCycleInList(); - - Head = cobj.InsertNode(Head, 3); - Head = cobj.InsertNode(Head, 2); - Head = cobj.InsertNode(Head, 0); - Head = cobj.InsertNode(Head, -4); - - cobj.DisplayList(Head); - cobj.CreateCycle(Head); - //cobj.DisplayList(Head); - temp = cobj.DetectCycle(Head); - if(temp == null) - { - System.out.println("Cycle Is Not Present In The Given Linked List !"); - } - else - { - System.out.println("Cycle Is Present In The Given Linked List !"); - } - } -} From 5afba7f5095c073e423622933bc1f27a8e79c34a Mon Sep 17 00:00:00 2001 From: Manish Tambe <52567320+manishtambe@users.noreply.github.com> Date: Wed, 5 Oct 2022 15:07:43 +0530 Subject: [PATCH 3/4] Delete PalindromeList.java --- Miscellaneous/PalindromeList.java | 108 ------------------------------ 1 file changed, 108 deletions(-) delete mode 100644 Miscellaneous/PalindromeList.java diff --git a/Miscellaneous/PalindromeList.java b/Miscellaneous/PalindromeList.java deleted file mode 100644 index 2b26a15..0000000 --- a/Miscellaneous/PalindromeList.java +++ /dev/null @@ -1,108 +0,0 @@ -// Given the head of a singly linked list, return true if it is a palindrome or false otherwise. - -// Example 1: -// Sample Input: -// Input: head = [1,2,2,1] -// Output: Output: true - -import java.util.*; - -class Node{ - int Data; - Node next; -}; - -public class PalindromeList { - public Node InsertNode(Node Head, int iNo) - { - Node newn = new Node(); - newn.Data = iNo; - newn.next = null; - - if(Head == null) - { - Head = newn; - } - else - { - Node temp = Head; - while(temp.next != null) - { - temp = temp.next; - } - temp.next = newn; - newn.next = null; - } - return Head; - } - - public boolean CompareEle(Node Head, Stack snode) - { - boolean bflag = true; - Node temp = Head; - int iData1; - int iData2; - - while(temp != null) - { - iData1 = snode.pop(); - iData2 = temp.Data; - if(iData1 != iData2) - { - bflag = false; - } - temp = temp.next; - } - return bflag; - } - - public boolean isPalindrome(Node head) { - Node temp; - temp = head; - boolean iRet = false; - Stack snode = new Stack(); - - while(temp != null) - { - //System.out.print("|" + temp.data + "| ->\t"); - snode.add(temp.Data); - temp = temp.next; - } - - iRet = CompareEle(head, snode); - - if(iRet == true) - { - iRet = true; - } - else - { - iRet = false; - } - - return iRet; - } - - public static void main(String args[]) { - Node Head = null; - PalindromeList pobj = new PalindromeList(); - boolean iRet = false; - - Head = pobj.InsertNode(Head, 1); - Head = pobj.InsertNode(Head, 2); - Head = pobj.InsertNode(Head, 2); - Head = pobj.InsertNode(Head, 1); - - iRet = pobj.isPalindrome(Head); - - if(iRet == true) - { - System.out.println("Entered Linked List Is Palindrome !"); - } - else - { - System.out.println("Entered Linked List Is Not Palindrome !"); - } - - } -} From 43397025f72973f541b03d4d4ed6fa7dbde07e50 Mon Sep 17 00:00:00 2001 From: Manish Tambe <52567320+manishtambe@users.noreply.github.com> Date: Wed, 5 Oct 2022 15:09:18 +0530 Subject: [PATCH 4/4] Add files via upload Did changes moved file in the data structure folder ! --- Data_Structures/DetectCycleInList.java | 128 +++++++++++++++++++++++++ Data_Structures/PalindromeList.java | 108 +++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 Data_Structures/DetectCycleInList.java create mode 100644 Data_Structures/PalindromeList.java diff --git a/Data_Structures/DetectCycleInList.java b/Data_Structures/DetectCycleInList.java new file mode 100644 index 0000000..66de734 --- /dev/null +++ b/Data_Structures/DetectCycleInList.java @@ -0,0 +1,128 @@ +// Problem Statement: - +// Given head, the head of a linked list, determine if the linked list has a cycle in it. +// There is a cycle in a linked list if there is some node in the list that can be reached +// again by continuously following the next pointer. Internally, pos is used to denote the +// index of the node that tail's next pointer is connected to. Note that pos is not passed +// as a parameter. +// Return true if there is a cycle in the linked list. Otherwise, return false. + +// Example 1: +// Sample Input : +// head = [3,2,0,-4], pos = 1 +// Output: true +// Explanation: There is a cycle in the linked list, where the tail connects to +// the 1st node (0-indexed). + +import java.util.*; + +class Node{ + int Data; + Node next; +}; + +public class DetectCycleInList { + public Node InsertNode(Node Head, int iNo) + { + Node newn = new Node(); + newn.Data = iNo; + newn.next = null; + + if(Head == null) + { + Head = newn; + } + else + { + Node temp = Head; + while(temp.next != null) + { + temp = temp.next; + } + temp.next = newn; + newn.next = null; + } + return Head; + } + + public void DisplayList(Node Head) + { + Node temp = Head; + + while(temp != null) + { + System.out.print("|"+temp.Data+"| ->"); + temp = temp.next; + } + System.out.print(" NULL\n"); + } + + public void CreateCycle(Node Head) + { + Node temp = Head; + Node temp1 = Head.next.next; + temp.next.next.next.next = temp1; + + } + + public Node DetectCycle(Node Head) + { + HashMap address = new HashMap<>(); + Node temp = Head; + int i = 0; + while(temp != null) + { + if(address.containsKey(temp)) + { + return temp; + } + else + { + address.put(temp, i); + i++; + } + temp = temp.next; + } + return temp; + } + + public boolean Result(Node Head) + { + boolean flag = false; + Node temp = DetectCycle(Head); + if(temp == null) + { + flag = false; + } + else + { + flag = true; + } + + return flag; + } + + public static void main(String args[]) + { + Node Head = null; + Node temp = null; + DetectCycleInList cobj = new DetectCycleInList(); + + Head = cobj.InsertNode(Head, 3); + Head = cobj.InsertNode(Head, 2); + Head = cobj.InsertNode(Head, 0); + Head = cobj.InsertNode(Head, -4); + + cobj.DisplayList(Head); + cobj.CreateCycle(Head); + //cobj.DisplayList(Head); + temp = cobj.DetectCycle(Head); + if(temp == null) + { + System.out.println("Cycle Is Not Present In The Given Linked List !"); + } + else + { + System.out.println("Cycle Is Present In The Given Linked List !"); + } + } +} diff --git a/Data_Structures/PalindromeList.java b/Data_Structures/PalindromeList.java new file mode 100644 index 0000000..2b26a15 --- /dev/null +++ b/Data_Structures/PalindromeList.java @@ -0,0 +1,108 @@ +// Given the head of a singly linked list, return true if it is a palindrome or false otherwise. + +// Example 1: +// Sample Input: +// Input: head = [1,2,2,1] +// Output: Output: true + +import java.util.*; + +class Node{ + int Data; + Node next; +}; + +public class PalindromeList { + public Node InsertNode(Node Head, int iNo) + { + Node newn = new Node(); + newn.Data = iNo; + newn.next = null; + + if(Head == null) + { + Head = newn; + } + else + { + Node temp = Head; + while(temp.next != null) + { + temp = temp.next; + } + temp.next = newn; + newn.next = null; + } + return Head; + } + + public boolean CompareEle(Node Head, Stack snode) + { + boolean bflag = true; + Node temp = Head; + int iData1; + int iData2; + + while(temp != null) + { + iData1 = snode.pop(); + iData2 = temp.Data; + if(iData1 != iData2) + { + bflag = false; + } + temp = temp.next; + } + return bflag; + } + + public boolean isPalindrome(Node head) { + Node temp; + temp = head; + boolean iRet = false; + Stack snode = new Stack(); + + while(temp != null) + { + //System.out.print("|" + temp.data + "| ->\t"); + snode.add(temp.Data); + temp = temp.next; + } + + iRet = CompareEle(head, snode); + + if(iRet == true) + { + iRet = true; + } + else + { + iRet = false; + } + + return iRet; + } + + public static void main(String args[]) { + Node Head = null; + PalindromeList pobj = new PalindromeList(); + boolean iRet = false; + + Head = pobj.InsertNode(Head, 1); + Head = pobj.InsertNode(Head, 2); + Head = pobj.InsertNode(Head, 2); + Head = pobj.InsertNode(Head, 1); + + iRet = pobj.isPalindrome(Head); + + if(iRet == true) + { + System.out.println("Entered Linked List Is Palindrome !"); + } + else + { + System.out.println("Entered Linked List Is Not Palindrome !"); + } + + } +}