-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay24-More-Linked-Lists
More file actions
43 lines (34 loc) · 1.36 KB
/
Day24-More-Linked-Lists
File metadata and controls
43 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Problem:
Objective
Check out the Tutorial tab for learning materials and an instructional video!
Task
A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next,
pointing to another node (i.e.: the next node in a list).
A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter.
Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.
Note: The head pointer may be null, indicating that the list is empty.
Be sure to reset your next pointer when performing deletions to avoid breaking the list.
Solution:
public static Node removeDuplicates(Node head) {
//Write your code here
Node currentNode = head;
while(currentNode!=null && currentNode.next!=null)
{
Node node = currentNode;
while(node.next!=null)
{
if(node.next.data==currentNode.data)
{
Node next = node.next.next;
Node temp= node.next;
node.next=next;
temp=null;
}
else{
node=node.next;
}
}
currentNode=currentNode.next;
}
return head;
}