Reverse a doubly linked list

Sort by

recency

|

20 Discussions

|

  • + 0 comments

    Java

    Iterative

    public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) {
    	DoublyLinkedListNode newHead = null;
    	while(llist != null){
    		DoublyLinkedListNode next = llist.next;
    		llist.next = newHead;
    		llist.prev = next;
    		newHead = llist;
    		llist = next;
    	}
    	return newHead;
    }
    

    Recursive:

    public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) {
    	return helper(null, llist);
    }
    
    private static DoublyLinkedListNode helper(DoublyLinkedListNode prev, DoublyLinkedListNode node){
    	if(node == null)
    		return prev;
    	DoublyLinkedListNode next = node.next;
    	node.next = prev;
    	node.prev = next;
    	return helper(node, next);
    }
    
  • + 0 comments

    This and "reverse a linked list" code is bugged from the beggining in Java 8 (the only Java that has autocomplete), is there any fu__ing checking in this website ?

  • + 0 comments

    Java 15 O(n)

    public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) {
            DoublyLinkedListNode current = llist;
            DoublyLinkedListNode prev = null;
    
            while (current != null) {
                DoublyLinkedListNode nextNode = current.next;
                current.next = prev;
                current.prev = nextNode; // swap prev and next pointers
                prev = current;
                current = nextNode;
            }
    
            //prev points to the new head of the reversed list
            return prev;
        }
    
  • + 0 comments

    Kotlin code is bugged and cannot be solved in this language!

  • + 0 comments

    Bugs in the output code in Kotlin make it impossible to complete this and other exercises. Why is the output code locked from editing?!?!