Question

Implement a function that detects if there is a “loop” in a singly linked list. A...

Implement a function that detects if there is a “loop” in a singly linked list. A “loop” is defined as a link from one node to itself or to another node that is before the node in the list. The function may only make one pass through the list. It must return 1 if a “loop” exists; 0 otherwise. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!

Homework Answers

Answer #1

import java.util.*;

//class node
class Node{
   public Node next;
   public int data;
   Node(int d,Node n )
   {
       data=d;
       next=n;
   }
}

// class linked list
class LinkedListImplementation{
  
public Node head;
   LinkedListImplementation()
   {
       head=null;
   }
  
   // function that insert the node
   public void Insert(int e)
   {
       Node newNode=new Node(e,null);
       if(head==null)
       {
           head=newNode;
       }
      
       else if(head.next==null) {
           head.next=newNode;
       }
       else
       {
           Node temp=head;
           while(temp.next!=null)
           {
               temp=temp.next;
           }
           temp.next=newNode;
       }
   }
  
  
   //display the list
   public void displayList() {
       Node temp=head;
       while(temp!=null) {
           System.out.print(temp.data+"->");
           temp=temp.next;
       }
       System.out.println();
   }
  
   //function that detect the loop
   public int detectLoop() {
       Node fast=head;
       Node slow=head;
       while((fast!=null) && (fast.next!=null) && (slow!=null))
       {
           fast=fast.next.next;
           if(slow==fast)
               return 1;
           slow=slow.next;
       }
       return 0;
   }
  
   //function that create the loop
   public void createLoop() {
       Node temp=head;
       while(temp.next!=null) {
           temp=temp.next;
       }
       temp.next=head;
   }
}

public class LinkedList {
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
       LinkedListImplementation list=new LinkedListImplementation();
       char ch;
       // insert the element in the linked list
       do {
           System.out.print("Enter the node");
           int el=sc.nextInt();
           list.Insert(el);
           System.out.print("Want to insert more than press Y/N");
           ch=sc.next().charAt(0);
       }while(ch=='y'||ch=='Y');
       System.out.println("Linked List ");
       // display the linked list
       list.displayList();
      
       System.out.println(list.detectLoop());//when loop is not created
       list.createLoop();//create the loop
       System.out.println(list.detectLoop());// after loop has been created
      
   }

  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does not have tail? Write the code. How much time does it take to Do it?
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
1.Implement a function which will -       + Accept a number, num, and a list, my_list...
1.Implement a function which will -       + Accept a number, num, and a list, my_list as arguments       + return True if num exists in my_list       + return False otherwise 2. Write Python code to implement the following function - def find_in_list(item, my_list):     """     Look for an item from my_list (of items.)     If the item is found, return the index position of the item;     otherwise, return -1.     """       #add your code here...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT