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
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.
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
Write a Python program prints out the unique elements of a list lst in a sorted...
Write a Python program prints out the unique elements of a list lst in a sorted manner (i.e it removes duplicates from the list). For example, if list is [10,20,30,20,10,50,60,40,80,50,40], the output should be [10, 20, 30, 40, 50, 60, 80].?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT