java help! part2: what is the time complexity of this algorithm? 13. (15 points)
ID: 3755357 • Letter: J
Question
java help!
part2: what is the time complexity of this algorithm?
13. (15 points) White a method removeDuplicates(), which when called by a SortedLinkedList. removes all duplicate items from the SortedLinkedList. You can assume the LinkedList is sorted in increasing order and you don't have to check if the list is empty. You can also assume (for the sake of simplicity) that the LinkedList has only numbers. // This is an instance method /I So you have access to head, tail (optional), and the Node class public void removeDuplicates ()Explanation / Answer
!.Algorithm
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If data of next node is same as current node then delete the next node. Before we delete a node, we need to store next pointer of the node
Code:
// Java program to remove duplicates from a sorted linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
void removeDuplicates()
{
/*Another reference to head*/
Node current = head;
/* Pointer to store the next pointer of a node to be deleted*/
Node next_next;
/* do nothing if the list is empty */
if (head == null)
return;
/* Traverse list till the last node */
while (current.next != null) {
/*Compare current node with the next node */
if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null;
current.next = next_next;
}
else // advance if no deletion
current = current.next;
}
}
/* Utility functions */
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
/* Drier program to test above functions */
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);
System.out.println("List before removal of duplicates");
llist.printList();
llist.removeDuplicates();
System.out.println("List after removal of elements");
llist.printList();
}
}
Output:
Complexity:
For this you should to trverse till last node and in worst case there should be all the nodes with same value so you should have to check and delete n-1 so complexity is O(n).
// Java program to remove duplicates from a sorted linked list
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
void removeDuplicates()
{
/*Another reference to head*/
Node current = head;
/* Pointer to store the next pointer of a node to be deleted*/
Node next_next;
/* do nothing if the list is empty */
if (head == null)
return;
/* Traverse list till the last node */
while (current.next != null) {
/*Compare current node with the next node */
if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null;
current.next = next_next;
}
else // advance if no deletion
current = current.next;
}
}
/* Utility functions */
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
/* Drier program to test above functions */
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);
System.out.println("List before removal of duplicates");
llist.printList();
llist.removeDuplicates();
System.out.println("List after removal of elements");
llist.printList();
}
}
Output:
Linked list before duplicate removal 11 11 11 13 13 20 Linked list after duplicate removal 11 13 20
Complexity:
For this you should to trverse till last node and in worst case there should be all the nodes with same value so you should have to check and delete n-1 so complexity is O(n).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.