LeetCode 3294: Convert Doubly Linked List to Array II Solution

Master LeetCode problem 3294 (Convert Doubly Linked List to Array II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3294. Convert Doubly Linked List to Array II

Problem Explanation

Explanation:

To convert a doubly linked list to an array, we can traverse the linked list while adding each node's value to the array. We will keep track of the head node and iterate until we reach the end of the linked list. Finally, we will return the array containing the values of the doubly linked list nodes.

  • Algorithm:

    1. Initialize an empty array to store the values of the doubly linked list nodes.
    2. Traverse the doubly linked list from the head node.
    3. For each node, add its value to the array.
    4. Return the array containing the values of the doubly linked list nodes.
  • Time Complexity: O(n) where n is the number of nodes in the doubly linked list.

  • Space Complexity: O(n) for the output array.

:

Solution Code

public int[] convertDoublyLinkedListToArray(DoublyListNode head) {
    List<Integer> list = new ArrayList<>();
    DoublyListNode current = head;
    while (current != null) {
        list.add(current.val);
        current = current.next;
    }
    int[] result = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        result[i] = list.get(i);
    }
    return result;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3294 (Convert Doubly Linked List to Array II)?

This page provides optimized solutions for LeetCode problem 3294 (Convert Doubly Linked List to Array II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3294 (Convert Doubly Linked List to Array II)?

The time complexity for LeetCode 3294 (Convert Doubly Linked List to Array II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3294 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3294 in Java, C++, or Python.

Back to LeetCode Solutions