## JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

# Program: Remove duplicates from sorted linked list

 Problem Description: Write a program to remove duplicates from sorted linked list. The linked list can be sorted in any order. Algorithm: Traverse from head node. Compare the current node with its next node. If the data of the next node is same as current nodes data, then delete the next node. Update current nodes next pointer to next nodes next value.

 LinkedListRemoveDuplicates ```package com.java2novice.algos; public class LinkedListRemoveDuplicates { static class Node { int data; Node next; Node(int tmp) {data = tmp;} } /** * Traverse through linked list and print each * nodes value * @param head */ static void printLinkedList(Node head) { while(head != null) { System.out.print(head.data+" "); head = head.next; } } static void deleteDuplicates(Node head) { // copy head node to temp current node Node curr = head; // Traverse the linked list while(curr != null) { // copy current node to temp node. Node temp = curr; /** * Compare the current node with next nodes data and * keep on continue comparing as long as current nodes * data is matching with other nodes. */ while(temp != null && temp.data == curr.data) { temp = temp.next; } /** * Set current node next pointer to the node which has * different data value. */ curr.next = temp; curr = curr.next; } } public static void main(String[] a) { Node n1 = new Node(23); Node n2 = new Node(29); Node n3 = new Node(29); Node n4 = new Node(45); Node n5 = new Node(45); Node n6 = new Node(45); Node n7 = new Node(54); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n6.next = n7; System.out.print("Initial linked list data: "); printLinkedList(n1); deleteDuplicates(n1); System.out.println(); System.out.print("Linked list data after removing duplicates: "); printLinkedList(n1); } } ```

 Output: ```Initial linked list data: 23 29 29 45 45 45 54 Linked list data after removing duplicates: 23 29 45 54 ```

#### List Of All Interview Programs:

Java2Novice - YouTube Channel
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.