reverseTheDLL
unknown
java
4 years ago
978 B
4
Indexable
//function to revrese the dll
public DLL reverseTheDLL(){
//i use tmp as current pointer
DLLNode<T> tmp=tail;
DLL<T> rev = new DLL<>();
//checking if its not empty
while(tmp!=null){
//adding tmp.info to the new DLL at head postion
rev.addToHead(tmp.info);
//we need so we can traverse
tmp=tmp.prev;
}
return rev;
}
public void reverseTheDLL2(){
//i use tmp as current pointer
DLLNode<T> tmp,placeholder=null;
//starting at tail
tmp=tail;
while(tmp!=null){
//store .prev
placeholder=tmp.prev;
tmp.prev=tmp.next;
tmp.next=placeholder;
//since tmp.next now point at prev we use it to traverse
tmp=tmp.next;
}
placeholder=tail;
tail=head;
head = placeholder;
}Editor is loading...