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.
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 ---->...
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
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...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows classic indexing from 0 to item_count - 1, and there is a node at position 2.
Develop an Algorithm and java program using Design Recipe: 1) Develop a java program to the...
Develop an Algorithm and java program using Design Recipe: 1) Develop a java program to the content of one file to another. Hint 1: read() Hint 2: write() 2) Develop a java program to List all files in a given directory. Hint: list()
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT