On my quest to understand data structures , i started implementing them in java.The time complexity for deleteAll would be O(n + n^2).How can i improve the deleteAll method ?
/*
* Singly linked list
*/
package linkedlisttest;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
class LinkedList {
Node head;
int size;
/**
*
* #param data element to add to list
* Time Complexity : O(n)
*/
public void add(int data) {
if (head == null) {
head = new Node(data);
size += 1;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
size += 1;
}
/**
*
* #return size of list
* Time Complexity: O(1)
* This is because we use a class
* variable size to keep track of size of linked list
*/
public int getSize() {
return size;
}
/**
*
* #param data element to insert
* #param index position at which to insert the element (zero based)
* Time Complexity : O(n)
*/
public void add(int data, int index) {
if (index > getSize()) {
return; // invalid position
}
Node current = head; //iterate through whole list
int pos = 0;
Node newNode = new Node(data);
if (index == 0) // special case, since its a single reference change!
{
newNode.next = head;
head = newNode; // this node is now the head
size += 1;
return;
}
while (current.next != null) {
if (pos == index - 1) {
break;
}
pos++;
current = current.next;
}
// These are 2 reference changes, as compared to adding at index 0
newNode.next = current.next; // here we are changing a refernce
current.next = newNode; // changing a reference here as well
size += 1;
}
/**
* Find the first occurrence of an element
* #param data element to find
* #return index at which element is found , -1 if not exist
* Time Complexity: O(n)
*/
public int find(int data) {
Node current = head;
int pos = 0;
int index = -1;
if(head == null) { //empty list
return index;
}
while(current != null) {
if (current.data == data) {
index = pos;
break;
}
pos++;
current = current.next;
}
return index;
}
/**
* Delete the first occurrence of data
* #param data element to delete
* Time complexity : O(n)
*/
public void delete(int data) {
Node current = head;
if (head == null) { // list is empty
return;
}
if(head.data == data) { // if we want to delete the head , make next node head
head = head.next;
size -= 1;
return;
}
while(current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
size -= 1;
return;
}
current = current.next;
}
}
/**
* Delete all occurrences of data
* #param data element to delete
*
*/
public void deleteAll(int data) {
Node current = head;
if (head == null) { // list is empty
return;
}
//while loop to delete consecutive occurances of data
while(head.data == data) { // if we want to delete the head , make next node head
head = head.next;
size -= 1;
}
while(current.next != null) {
//while loop to delete consecutive occurances of data
while (current.next.data == data) {
current.next = current.next.next;
size -= 1;
}
current = current.next;
}
}
public void reverse() {
}
/**
* Prints the whole linked list
* Time Complexity : O(n)
*/
public void print() {
if(head == null) { //list is empty
return;
}
Node current = head;
while (current.next != null) {
System.out.print(current.data + "->");
current = current.next;
}
System.out.print(current.data + "\n");
}
}
public class LinkedListTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
LinkedList lt = new LinkedList();
lt.print();
lt.add(3);
lt.add(5);
lt.add(6);
lt.print();
lt.add(4, 1);
lt.print();
lt.add(4, 7);// 7 is an invalid index
lt.add(8, 3);
lt.add(8, 4);
lt.print();
System.out.println("Position : " + lt.find(8));
lt.delete(5);
lt.print();
lt.deleteAll(8);
lt.print();
System.out.println("Size : " + lt.getSize());
}
}
The time complexity of your implementation is O(n), not O(n + n^2) as you believed.
Although a nested loop is a common sign of O(n^2) it's not always the case.
The important point is the number of iterations.
In your case,
if you take k steps in the nested loop,
that reduces the remaining steps in the outer loop by k.
Overall,
you will still be taking n steps to reach the end.
But your implementation has some bugs,
and can also be improved:
Bug: you assign current = head, but if head.data == data then it will be deleted, and current will still point to it
There is no need for a nested loop. After the special treatment for the head, you can simply follow the nodes and delete the matching items
Like this:
public void deleteAll(int data) {
while (head != null && head.data == data) {
head = head.next;
size -= 1;
}
if (head == null) {
return;
}
Node current = head;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
size -= 1;
} else {
current = current.next;
}
}
}
By the way, the special treatment of the head can be a bit annoying.
An elegant alternative is to create a dummy that points to head:
public void deleteAll(int data) {
Node dummy = new Node();
dummy.next = head;
Node current = dummy;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
size -= 1;
} else {
current = current.next;
}
}
head = dummy.next;
}
Note that everything in Java is reference except those primitive types. So current still stays at the original head.
It is unnecessary to treat consecutive occurrences as special cases. A simple iterate&judge through the linked list with O(n) - linear complexity will do exactly what you want.
Besides, a nested loop doesn't necessarily means it will definitely cost O(n^2). If you move current every times, it still walk through the linked list in linear time.
Another personal suggestion: If you need to write something like node.next.next when dealing with linked list, it's time to set another reference or to refactor your code.
Related
I'm on HackerRank and I need to remove duplicate items from a sorted linked list. I passed all the cases except for two of them which the input is something like: 10001102034
So my program takes to seconds to complete and exceed the time. How can I do my code more efficiently, I heard about using square root but I don't know how to use it. Any guide is appreciate. Here is my code.
private static Node removeDuplicates(Node head) {
/* Another reference to head */
Node current = head;
Node next;
/* Traverse list till the last node */
while (current != null && current.next != null) {
if (current.data == current.next.data) {
next = current.next.next;
if (next == null) {
current.next = null;
break;
}
current.next = next;
} else {
current = current.next;
}
}
return head;
}
Again. It works but takes too much times with longer numbers.
You should replace condition if (current.data == current.next.data) with while loop and use break 'label':
out:
while (current != null && current.next != null) {
while (current.data == current.next.data) {
next = current.next.next;
if (next == null) {
current.next = null;
break out;
}
current.next = next;
}
current = current.next;
}
You can't use the square root because when u want to remove duplicates from a list you have to check all the list .
The square root technique is used for searching in a sorted list.
But for your question if you can improve the runtime on that your code in O(n^2) but if you change your code to use hashtable you can make it O(n).
import java.util.HashSet;
public class removeDuplicates
{
static class node
{
int val;
node next;
public node(int val)
{
this.val = val;
}
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a given linked list */
static void printList(node head)
{
while (head != null)
{
System.out.print(head.val + " ");
head = head.next;
}
}
I hope this will help you.
Got an assignment to create a class that links elements together.
Methods get(), add() and remove() were predefined by the assignment.
I actually managed to write code that creates such linked list,
but with the exception that the instance with value "Yo!" gets overwritten, when
adding new elements to the list and also I can't get the remove method working.
I really can't wrap my head around referencing object this way.
Could you help me correct my code ?
/**
*
* Represents a linked list of elements.
*
* #param <T>
*/
class LinkedElement<T> {
/**
* Adds a new linked element holding the given value at the end of the linked
* elements.
*
* #param newVal the new value.
*/
public void add(T newVal) {
if (head == null) {
head = new Element(newVal);
}
Element next = new Element(newVal);
Element current = head;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(next);
}
increaseListSize();
}
/**
* Removes the i-th element from the linked elements. If {#code i == 0}, this
* will effectively remove the head element. Thus, this method returns the
* linked element that is the new head element.
*
* #param i index of the element to remove.
* #return the new head element.
*/
public LinkedElement<T> remove(int i) {
if (i < 1 || i > getListSize())
return null;
Element current = head;
if (head != null) {
for (int e = 0; e < i; i++) {
if (current.getNext() == null)
return null;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
decreaseListSize();
}
return null;
}
Your get method is slightly wrong, you need to start current with head and not head.next()
public T get(int i) {
if (i < 0)
return null;
Element current = head;
if (current != null) {
for (int e = 0; e < i; e++) {
if (current.getNext() == null)
return null;
current = current.getNext();
}
return current.getValue();
}
return null;
}
You have a mistake in your get method: Element current = head; is correct.
There are several things that doesn't work in your remove method. This one should be working:
public void remove(int i) {
if (i==0) {
head = head.getNext();
decreaseListSize();
return;
}
if (i < 1 || i > getListSize()) {
return;
}
Element current = head;
if (head != null) {
for (int e = 1; e < i; e++) {
if (current.getNext() == null)
return ;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
decreaseListSize();
}
}
Note that I changed return type to void since your method returned null in any case and return of head is no neccessary. If you want to return the head element you can easily adapt it and return head instead of nothing.
Furthermore, a note that the count - as so often in computer science - starts at 0. To remove the first element you have to write headElement.remove(0);.
I found this code for adding an item to the front of the linked list, but since I have a last node, it doesn't work quite right, so I changed it a tiny bit:
public void moveToFront(String node) {
DoubleNode previous = first;
temp = first;
while (temp != null) {
if (node.equals(temp.item)) {
//Found the item
previous.next = temp.next;
temp.next = first;
first = temp;
if (last.next != null) {
last = last.prev;
last.prev = previous;
}
return;
}
previous = temp;
temp = temp.next;
}
The if (last.next != null) is asking if the original last was moved, and checking if the new last has the right links. Now I think it works properly for my code.
I'd like to implement this code, but for adding an item to the end. However, last just isn't right now. When calling last.prev it only gets the one item behind it, but last.prev.prev to infinity is the same item.
My idea was instead of working from first like in moveToFront(), I work from last, and step through each node backwards, but obviously that doesn't work when last doesn't work anymore.
public void moveToEnd(String node) {
DoubleNode previous = last;
temp = last;
System.out.println("last = " + last.prev.item);
System.out.println("temp = " + temp.item);
while (!temp.item.equals(first.item)) {
if(node.equals(temp.item)){
System.out.println("previous.prev = " + previous.prev.item);
}
previous = temp;
temp = temp.prev;
System.out.println("temp.prev = " + temp.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.item);
}
Here's how I implement my linked list:
public class LinkedListDeque {
public DoubleNode first = new DoubleNode(null);
public DoubleNode last = new DoubleNode(null);
public DoubleNode temp;
public int N;
LinkedListDeque() {
first.next = last;
last.prev = first;
}
private class DoubleNode {
String item;
int counter = 0;
DoubleNode next;
DoubleNode prev;
DoubleNode(String i) {
this.item = i;
}
}
I found this example of a complete doubly linked list. It does not have an add to front method, but it is adding to the back of the linked list each time. Hopefully, it will help and give you a better idea of how this data structure is supposed to work and function. I would definitely test it first as it states in the readme for this GitHub that none of the code has been tested. Sorce
/*******************************************************
* DoublyLinkedList.java
* Created by Stephen Hall on 9/22/17.
* Copyright (c) 2017 Stephen Hall. All rights reserved.
* A Linked List implementation in Java
********************************************************/
package Lists.Doubly_Linked_List;
/**
* Doubly linked list class
* #param <T> Generic type
*/
public class DoublyLinkedList<T extends Comparable<T>> {
/**
* Node class for singly linked list
*/
public class Node{
/**
* private Members
*/
private T data;
private Node next;
private Node previous;
/**
* Node Class Constructor
* #param data Data to be held in the Node
*/
public Node(T data){
this.data = data;
next = previous = null;
}
}
/**
* Private Members
*/
private Node head;
private Node tail;
private int count;
/**
* Linked List Constructor
*/
public DoublyLinkedList(){
head = tail = null;
count = 0;
}
/**
* Adds a new node into the list with the given data
* #param data Data to add into the list
* #return Node added into the list
*/
public Node add(T data){
// No data to insert into list
if (data != null) {
Node node = new Node(data);
// The Linked list is empty
if (head == null) {
head = node;
tail = head;
count++;
return node;
}
// Add to the end of the list
tail.next = node;
node.previous = tail;
tail = node;
count++;
return node;
}
return null;
}
/**
* Removes the first node in the list matching the data
* #param data Data to remove from the list
* #return Node removed from the list
*/
public Node remove(T data){
// List is empty or no data to remove
if (head == null || data == null)
return null;
Node tmp = head;
// The data to remove what found in the first Node in the list
if(equalTo(tmp.data, data)) {
head = head.next;
count--;
return tmp;
}
// Try to find the node in the list
while (tmp.next != null) {
// Node was found, Remove it from the list
if (equalTo(tmp.next.data, data)) {
if(tmp.next == tail){
tail = tmp;
tmp = tmp.next;
tail.next = null;
count--;
return tmp;
}
else {
Node node = tmp.next;
tmp.next = tmp.next.next;
tmp.next.next.previous = tmp;
node.next = node.previous = null;
count--;
return node;
}
}
tmp = tmp.next;
}
// The data was not found in the list
return null;
}
/**
* Gets the first node that has the given data
* #param data Data to find in the list
* #return Node First node with matching data or null if no node was found
*/
public Node find(T data){
// No list or data to find
if (head == null || data == null)
return null;
Node tmp = head;
// Try to find the data in the list
while(tmp != null) {
// Data was found
if (equalTo(tmp.data, data))
return tmp;
tmp = tmp.next;
}
// Data was not found in the list
return null;
}
/**
* Gets the node at the given index
* #param index Index of the Node to get
* #return Node at passed in index
*/
public Node indexAt(int index){
//Index was negative or larger then the amount of Nodes in the list
if (index < 0 || index > size())
return null;
Node tmp = head;
// Move to index
for (int i = 0; i < index; i++)
tmp = tmp.next;
// return the node at the index position
return tmp;
}
/**
* Gets the current count of the array
* #return Number of items in the array
*/
public int size(){
return count;
}
/**
* Determines if a is equal to b
* #param a: generic type to test
* #param b: generic type to test
* #return boolean: true|false
*/
private boolean equalTo(T a, T b) {
return a.compareTo(b) == 0;
}
}
There is a problem with your moveToFront() method. It does not work for if node == first. If the node that needs to be moved is the first node, you end up setting first.next = first which is incorrect. The below code should work
public void moveToFront(DoubleNode node) {
DoubleNode previous = first;
temp = first;
while (temp != null && node != first) {
if (node.equals(temp.item)) {
//Found the item
previous.next = temp.next;
temp.next = first;
first = temp;
if (last.next != null) {
last = last.prev;
last.prev = previous;
}
return;
}
previous = temp;
temp = temp.next;
}
Now coming to moveToLast() the following code should work
public void moveToLast(DoubleNode node) {
DoubleNode temp = first;
DoubleNode prev = new DoubleNode(null); //dummy sentinel node
while (temp != null && node != last) {
if (temp == node) {
if (temp == first) first = temp.next;
prev.next = temp.next;
if (temp.next != null) temp.next.prev = prev;
last.next = temp;
temp.prev = last;
last = temp;
last.next = null;
break;
}
prev = temp;
temp = temp.next;
}
}
// Complete the sortedInsert function below.
/*
* For your reference:
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode next;
* DoublyLinkedListNode prev;
* }
*
*/
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) {
DoublyLinkedListNode Leader=head;
DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
while(Leader.next!=null){
if(data>Leader.data){
Leader = Leader.next;
}
else {
if(Leader.prev == null) {
newNode.next = Leader;
Leader.prev = newNode;
head = newNode;
return head;
}
}
}
if(Leader.next == null) {
if(data<Leader.data) {
newNode.prev = Leader.prev;
newNode.next = Leader;
Leader.prev.next = newNode;
return head;
} else {
newNode.prev = Leader;
Leader.next = newNode;
return head;
}
}
return head;
}
in the above-sorted insert method how to decrease this doubly linked list complexity, this is a hackerrank question I'm getting timed outs for the test cases I need help in decreasing the time complexity for this code.
You code will never come out of while loop.
Lets take the example. List = [(1), (4), (4)](only 1 element). New node is (4). Your result should be [(1), (4), (4), (4)]. But lets walk your code and check what will happen. Initially Leader = (1)
while(Leader.next!=null){ // 1
if(data>Leader.data){ // 3
Leader = Leader.next;
}
else { // 6
if(Leader.prev == null) { // 7
newNode.next = Leader;
Leader.prev = newNode;
head = newNode;
return head;
}
}
}
At line 1 check will pass (as (1).next is not null; in fact it is (4)).
At line 3 ((4) > (1)). Check pass. Leader = (1).next = (4). Jump to line 1
At line 1 check will pass (as (4).next is not null; in fact it is (4)).
At line 3 ((4) > (4)). Check Fail. Enter line 7
At line 7 check will fail ((4).prev is not null; in fact it is (4) - 1st 4). No update in Leader will take place. Leader will remain same & you will enter infinte loop from here.
You will have to take care of this. Maybe the Problem's discussion page will help. But do give it a through try.
My own try is included below:
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) {
DoublyLinkedListNode n = new DoublyLinkedListNode();
n.data = data;
DoublyLinkedListNode curr = head;
if (head == null) {
return n;
}
// if given node is smaller than 1st node
if (data < curr.data) {
n.next = curr;
return n;
}
// find first node greater than given node
while (curr.next != null && curr.data < data) {
curr = curr.next;
}
// reached to the end.
if (curr.next == null && data >= curr.data) {
curr.next = n;
} else { // found the 1st node which is greater than given node
curr.prev.next = n;
n.next = curr;
}
return head;
}
I am learning data structures current and below is my implementation for linkedlist.I have kept it as simple as possible as my aim here is to understand the logic.
/*
* Singly linked list
*/
package linkedlisttest;
class Node {
int data;
Node next;
public Node(int data)
{
this.data = data;
}
}
class LinkedList {
Node head;
public void add(int data)
{
if (head == null)
{
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public int getSize() {
int i = 0;
Node current = head;
while (current != null) {
i += 1;
current = current.next;
}
return i;
}
public void add(int data, int index)
{
if (head == null && index == 0)
{
head = new Node(data);
return;
} else if (head == null && index != 0) {
return; // invalid position
} else if ( index > getSize() ) {
return;
}
Node current = head;
//iterate through whole list
int pos = -1;
Node previous = null;
Node next = null;
Node newNode = new Node(data);
//find next and previous nodes with relation to position
while (current != null) {
if (pos == index - 1) {
previous = current;
} else if (pos == index + 1) {
next = current;
}
pos++;
current = current.next;
}
previous.next = newNode;
newNode.next = next;
}
public void print()
{
Node current = head;
while (current.next != null) {
System.out.print(current.data + "->");
current = current.next;
}
System.out.print(current.data);
}
}
public class LinkedListTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
LinkedList lt = new LinkedList();
lt.add(3);
lt.add(5);
lt.add(6);
lt.add(4,1);
lt.print();
}
}
The bug happens for lt.add(4,1) and i suspect its an off by one error.
Expected output: 3->4->6
Actual output: 3->5->4
Thanks for the help guys...
Edit
Thanks to #StephenP and #rosemilk for their help.Indeed the code above has a logical bug as it replaces the value at index and not add it.
Here is the new optimized code
/*
* Singly linked list
*/
package linkedlisttest;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
class LinkedList {
Node head;
int size;
/**
*
* #param data element to add to list
* Time Complexity : O(n)
*/
public void add(int data) {
if (head == null) {
head = new Node(data);
size += 1;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
size += 1;
}
/**
*
* #return size of list
* Time Complexity: O(1)
* This is because we use a class
* variable size to keep track of size of linked list
*/
public int getSize() {
return size;
}
/**
*
* #param data element to insert
* #param index position at which to insert the element (zero based)
* Time Complexity : O(n)
*/
public void add(int data, int index) {
if (index > getSize()) {
return; // invalid position
}
Node current = head; //iterate through whole list
int pos = 0;
Node newNode = new Node(data);
if (index == 0) // special case, since its a single reference change!
{
newNode.next = head;
head = newNode; // this node is now the head
size += 1;
return;
}
while (current.next != null) {
if (pos == index - 1) {
break;
}
pos++;
current = current.next;
}
// These are 2 reference changes, as compared to adding at index 0
newNode.next = current.next; // here we are changing a refernce
current.next = newNode; // changing a reference here as well
size += 1;
}
/**
* Prints the whole linked list
* Time Complexity : O(n)
*/
public void print() {
if(getSize() == 0) { //list is empty
return;
}
Node current = head;
while (current.next != null) {
System.out.print(current.data + "->");
current = current.next;
}
System.out.print(current.data + "\n");
}
}
public class LinkedListTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
LinkedList lt = new LinkedList();
lt.print();
lt.add(3);
lt.add(5);
lt.add(6);
lt.print();
lt.add(4, 1);
lt.print();
lt.add(4, 7);// 7 is an invalid index
lt.add(8, 3);
lt.print();
}
}
Your add (int , int ) function has a logical bug and can be made better. You don't need previous current and next references, and can cleverly manipulate the list using just the reference to current node, handling inseration at index 0 separately. I would write the add function as follows
public void add(int data, int index)
{
if ( index > getSize() ) {
return; // invalid position
}
Node current = head; //iterate through whole list
int pos = 0;
Node newNode = new Node(data);
if (index == 0) // special case, since its a single reference change!
{
newNode.next = head;
head = newNode; // this node is now the head
return;
}
while (current.next != null) {
if (pos == index - 1) {
break;
}
pos++;
current = current.next;
}
// These are 2 reference changes, as compared to adding at index 0
newNode.next = current.next; // here we are changing a refernce
current.next = newNode; // changing a reference here as well
}
Also, your print function gives a NullPointerException when you try to print an empty list. I would write the print function like this,
public void print()
{
Node current = head;
while (current != null) {
System.out.print(current.data + "->");
current = current.next;
}
System.out.println("null"); // this is just to say last node next points to null!
}
Hope this helps :)
Currently, if you print out pos in your loop, the indices are -1, 0, 1 (instead of 0, 1, 2), so it'll never "find" the correct next. Replace int pos = -1; with int pos = 0; and it'll work.
I do agree with #StephenP that the output should (arguably) be 3->4->5->6, but that's a design decision.