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

/*If you any query do comment in the comment section else like the solution*/

class LinkedList
{
        Node head; 
        class Node
        {
                int val;
                Node link;
                Node(int d) { 
                        val = d; link = null; 
                }
        }

        void removeDuplicates()
        {
                
                Node curr = head;               
                while (curr != null) {
                        Node ptr = curr;
                                                
                        while(ptr!=null && ptr.val==curr.val) {
                                ptr = ptr.link;
                        }                       
                        curr.link = ptr;
                        curr = curr.link;
                }
        }                               
        public void push(int val)
        {       
                Node newNode = new Node(val);           
                newNode.link = head;
                head = newNode;
        }
        void printList()
        {
                Node ptr = head;
                while (ptr != null)
                {
                        System.out.print(ptr.val+" ");
                        ptr = ptr.link;
                } 
                System.out.println();
        }
        public static void main(String args[])
        {
                LinkedList llist = new LinkedList();
                llist.push(10);
                llist.push(3);
                llist.push(3);
                llist.push(3);
                llist.push(5);
                llist.push(6);
                llist.push(6);
                llist.push(3);
                llist.push(2);
                llist.push(2);
                System.out.print("Original List: ");
                llist.printList();
                llist.removeDuplicates();
                System.out.print("List after removal of duplicates: ");
                llist.printList();
        }
} 

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