Question

Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List....

Program in Java

1- Write a code to remove continuous repeatitive elements of a Linked List.

Example:

Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL

The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL

Homework Answers

Answer #1

Program to remove continuous repetitive elements of a Linked List in Java :

import java.util.*;

//Creating the class
class LinkedList
{
   //Initializing the head node
Node head;
//Creating the node class
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}

//Function to remove the continuous repetitive elements in the linked list
void removeRepetitive()
{
Node current = head;

//Comparing each node with its next node whether they are same or not
while (current != null) {
Node temp = current;
while(temp!=null && temp.data==current.data) {
temp = temp.next;
}
current.next = temp;
current = current.next;
}
}
//Function to push the elements to the linked list
public void push(int new_data)
{
   Node newNode = new Node(new_data);
newNode.next = head;
head = newNode;
}

//Just a normal function to print the given linked list
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}

//Main function
public static void main(String args[])
{
   //Creating the linked list and taking the size and elements of the linked list as input from the user
LinkedList linkedList = new LinkedList();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size :");
int n = sc.nextInt();
System.out.println("Enter the values :");
for(int i=0; i<n; i++) {
   int t = sc.nextInt();
   linkedList.push(t);
}   
System.out.println("List before removal of duplicates");
linkedList.printList();
  
//Calling the function to remove continuos repetitive elements
linkedList.removeRepetitive();
  
System.out.println("List after removal of elements");
linkedList.printList();
}
}

Program Screenshots :

Copying and pasting can result in improper code format. In that case, you can see the following screenshots for reference :

Output :

If you liked my answer, then please give me a thumbs up.

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
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List....
Program in Java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List....
program in java 1- Write a code to remove continuous repeatitive elements of a Linked List. Example: Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6 -> 6 -> 3 -> 2 -> 2 -> NULL The answer: -10 -> 3 -> 5 -> 6 -> 3 -> 2 -> NULL
Write a program to swap mth and nth elements of a linked list. Code needed in...
Write a program to swap mth and nth elements of a linked list. Code needed in java.
Write a java program using java.util package to add these elements to linked list {KKU, KSU,...
Write a java program using java.util package to add these elements to linked list {KKU, KSU, KAU, NU} then add -KAUSTI to index 2 and remove the KKU element.
Java Generic Linked List Problem: How do I remove a slice of a linked list and...
Java Generic Linked List Problem: How do I remove a slice of a linked list and add its data to another linked list in java? Example: Private<T> head; Private int size; Public List<T> slice(int from, int to) { // This method will remove the data from the given range (inclusive) and add it to a new List and return this new list. }
Write the java code that will convert the given Before links to the After links with...
Write the java code that will convert the given Before links to the After links with comments. For example: Before:            +----+----+    +----+----+ list ----> | 1 | +----> | 2 | / |            +----+----+    +----+----+ After:            +----+----+    +----+----+    +----+----+ list ----> | 1 | +----> | 2 | +----> | 3 | / |            +----+----+    +----+----+    +----+----+ Code: (example answer) list.next.next = new ListNode(3, null);   // 2 -> 3 (1) Before:            +----+----+    +----+----+ list ---->...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n elements. The program gets the head of the list (L.head) as input and cannot use any storage other than the given list. Don't not use recursive~~~~~~~ thanks
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT