Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

reverse a doubly linked list

public static DoublyLinkedListNode reverse(DoublyLinkedListNode node) {
        
        DoublyLinkedListNode newHead = null;
        do{
            // Last node is the new head now
            if(node.next == null){
                newHead = node;
            }
            
            DoublyLinkedListNode temp = node.prev;
            DoublyLinkedListNode nextNode  = null;
            
    // if node.next is null it means processing last node which is the new               //head
            if(node != null && node.next != null){
                nextNode = node.next;
                node.prev = nextNode;
            }
    // Creating prev next | next prev chaining 
            node.next = temp;
                
            if(node != null && nextNode != null)
                nextNode.prev = node;
            
            node = nextNode;
            
        }while(node != null);
        
        return newHead;
    }
Comment

PREVIOUS NEXT
Code Example
Java :: Java 2-dimensional Array 
Java :: print statement in java, simplest java program, Hello World in Java 
Java :: java packages example 
Java :: java 2d jagged array example 
Java :: get field name java 
Java :: java questions 
Java :: how to find a word in a statement java 
Java :: run java file 
Java :: math class in java 
Java :: set style programmatically android 
Java :: java delete element from list 
Java :: java switch tutorial 
Java :: limit decimals java 
Java :: java printwriter create file 
Java :: creating an object in java 
Java :: int arr = new int 
Java :: comments java 
Java :: can we have abstract class having no abstract method in java? 
Java :: menu alert dialog in android 
Java :: loop and save letters in a string java 
Java :: enum class in java 
Java :: Java Abstract Class and Method 
Java :: java "-" 
Java :: naming convention in selenium 
Java :: android activity set action bar options 
Java :: md5 java ee 
Java :: generate random color java 
Java :: jbutton default color 
Java :: what is java.io example 
Java :: java split not working on comma 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =