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 !"); + } + + } +}