Reverse a doubly linked list

Sort by

recency

|

22 Discussions

|

  • + 1 comment

    AGAIN!?

    public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist)
    {
        return llist;
    }
    

    Solution.cs(34,25): warning CS8625: Cannot convert null literal to non-nullable reference type. Solution.cs(35,25): warning CS8625: Cannot convert null literal to non-nullable reference type. Solution.cs(33,16): warning CS8618: Non-nullable field 'head' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Solution.cs(33,16): warning CS8618: Non-nullable field 'tail' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Solution.cs(24,25): warning CS8625: Cannot convert null literal to non-nullable reference type. Solution.cs(25,25): warning CS8625: Cannot convert null literal to non-nullable reference type. Solution.cs(22,16): warning CS8618: Non-nullable field 'next' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Solution.cs(22,16): warning CS8618: Non-nullable field 'prev' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. Solution.cs(110,43): error CS0103: The name 'reverse' does not exist in the current context

    These lines are not editable, so what the heck are you supposed to do?

    Solution.cs(96,50): warning CS8604: Possible null reference argument for parameter 'path' in 'StreamWriter.StreamWriter(string path, bool append)'.
    
  • + 0 comments

    My answer in Python

    def reverse(llist):
        prev = None
        current = llist
        while current:
            next_node = current.next
            current.next = prev
            current.prev = next_node
            prev = current
            current = next_node
        return prev
    
  • + 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;
        }