Traverse and reverse a linked list takes time complexity of O(n).
Traverse a double linked list requires only node next reference until it reach to trail or null value :
public void traverseLinkedList(){
Node<T> tempNode = start;
while (tempNode != null){
tempNode = tempNode.getNextReference();
Log.e("value", "" + tempNode.getValue());
}
}
Reverse a double linked list :
public Node<T> reverseLinkedList(){
Node<T> temp = null;
Node<T> current = start;
while (current.getNextReference() != null){
if (current == null){
break;
}else{
temp = current.getNextReference();
current.setNextReference(current.getPrevReference());
current.setPrevReference(temp);
current = temp;
}
}
return current;
}
0 Comment(s)