remove duplicates from singly linked list
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
/* Function to remove duplicates from an
unsorted linked list */
void remove_duplicates() {
Node ptr1 = null, ptr2 = null, dup = null;
ptr1 = head;
/* Pick elements one by one */
while (ptr1 != null && ptr1.next != null) {
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2.next != null) {
/* If duplicate then delete it */
if (ptr1.data == ptr2.next.data) {
/* sequence of steps is important here */
dup = ptr2.next;
ptr2.next = ptr2.next.next;
System.gc();
} else /* This is tricky */ {
ptr2 = ptr2.next;
}
}
ptr1 = ptr1.next;
}
}
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(10);
list.head.next = new Node(12);
list.head.next.next = new Node(11);
list.head.next.next.next = new Node(11);
list.head.next.next.next.next = new Node(12);
list.head.next.next.next.next.next = new Node(11);
list.head.next.next.next.next.next.next = new Node(10);
System.out.println("Linked List before removing duplicates : \n ");
list.printList(head);
list.remove_duplicates();
System.out.println("");
System.out.println("Linked List after removing duplicates : \n ");
list.printList(head);
}
}
Also in Java:
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- how to get the screen dimensions in java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- java bigdecimal compareto
- Category
- Java
- Title
- rewrite file java
- Category
- Java
- Title
- how to test how many of one character is in a string java
- Category
- Java
- Title
- character at index of string java
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- how to define an arraylist in java
- Category
- Java
- Title
- how to remove duplicates from an array java
- Category
- Java
- Title
- java 8 list stream delete by name
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- dates in java 8
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- java code to get all leaf nodes of a xml file
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- java startActivity crash
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- gson parse json
- Category
- Java
- Title
- in javascipt how to stop further page processing
- Category
- Java
- Title
- socket in java
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- labeled for loop in java
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- print a list java
- Category
- Java
- Title
- snackbar not showing with bottom nav
- Category
- Java
- Title
- sieve in java
- Category
- Java
- Title
- jar file not running after being compiled
- Category
- Java
- Title
- java parse xml string
- Category
- Java
- Title
- java file download
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- convert every character in string to arraylist jva
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- Java script code for add
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- java split array into two
- Category
- Java
- Title
- bigdecimal divide java
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- retrofit
- Category
- Java
- Title
- java list of a class has a string that is equal to
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- treeset java descending order using comparator
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- check if optional is empty java
- Category
- Java
- Title
- enum java
- Category
- Java
- Title
- write file java
- Category
- Java
- Title
- square root java
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- deserialize date java
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- how to format a double in java to 2 decimal places
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- final variables in java
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- spigot custom join message
- Category
- Java