Welcome to Subscribe On Youtube

3263. Convert Doubly Linked List to Array I 🔒

Description

You are given the head of a doubly linked list, which contains nodes that have a next pointer and a previous pointer.

Return an integer array which contains the elements of the linked list in order.

 

Example 1:

Input: head = [1,2,3,4,3,2,1]

Output: [1,2,3,4,3,2,1]

Example 2:

Input: head = [2,2,2,2,2]

Output: [2,2,2,2,2]

Example 3:

Input: head = [3,2,3,2,3,2]

Output: [3,2,3,2,3,2]

 

Constraints:

  • The number of nodes in the given list is in the range [1, 50].
  • 1 <= Node.val <= 50

Solutions

Solution 1: Direct Traversal

We can directly traverse the linked list, adding the values of the nodes to the answer array $\textit{ans}$ one by one.

After the traversal is complete, return the answer array $\textit{ans}$.

The time complexity is $O(n)$, where $n$ is the length of the linked list. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.

  • /*
    // Definition for a Node.
    class Node {
        public int val;
        public Node prev;
        public Node next;
    };
    */
    
    class Solution {
        public int[] toArray(Node head) {
            List<Integer> ans = new ArrayList<>();
            for (; head != null; head = head.next) {
                ans.add(head.val);
            }
            return ans.stream().mapToInt(i -> i).toArray();
        }
    }
    
    
  • /**
     * Definition for doubly-linked list.
     * class Node {
     *     int val;
     *     Node* prev;
     *     Node* next;
     *     Node() : val(0), next(nullptr), prev(nullptr) {}
     *     Node(int x) : val(x), next(nullptr), prev(nullptr) {}
     *     Node(int x, Node *prev, Node *next) : val(x), next(next), prev(prev) {}
     * };
     */
    class Solution {
    public:
        vector<int> toArray(Node* head) {
            vector<int> ans;
            for (; head; head = head->next) {
                ans.push_back(head->val);
            }
            return ans;
        }
    };
    
    
  • """
    # Definition for a Node.
    class Node:
        def __init__(self, val, prev=None, next=None):
            self.val = val
            self.prev = prev
            self.next = next
    """
    
    
    class Solution:
        def toArray(self, root: "Optional[Node]") -> List[int]:
            ans = []
            while root:
                ans.append(root.val)
                root = root.next
            return ans
    
    
  • /**
     * Definition for a Node.
     * type Node struct {
     *     Val int
     *     Next *Node
     *     Prev *Node
     * }
     */
    
    func toArray(head *Node) (ans []int) {
    	for ; head != nil; head = head.Next {
    		ans = append(ans, head.Val)
    	}
    	return
    }
    
    
  • /**
     * Definition for _Node.
     * class _Node {
     *     val: number
     *     prev: _Node | null
     *     next: _Node | null
     *
     *     constructor(val?: number, prev? : _Node, next? : _Node) {
     *         this.val = (val===undefined ? 0 : val);
     *         this.prev = (prev===undefined ? null : prev);
     *         this.next = (next===undefined ? null : next);
     *     }
     * }
     */
    
    function toArray(head: _Node | null): number[] {
        const ans: number[] = [];
        for (; head; head = head.next) {
            ans.push(head.val);
        }
        return ans;
    }
    
    

All Problems

All Solutions