跳转至

3294. Convert Doubly Linked List to Array II 🔒

题目描述

You are given an arbitrary node from 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,5], node = 5

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

Example 2:

Input: head = [4,5,6,7,8], node = 8

Output: [4,5,6,7,8]

 

Constraints:

  • The number of nodes in the given list is in the range [1, 500].
  • 1 <= Node.val <= 1000
  • All nodes have unique Node.val.

解法

方法一:遍历链表

我们可以从给定的节点开始,往前遍历链表,直到遍历到头节点,然后再从头节点开始往后遍历链表,将遍历到的节点的值添加到答案数组中。

遍历结束后,返回答案数组即可。

时间复杂度 $O(n)$,其中 $n$ 为链表的节点个数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
"""
# 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, node: "Optional[Node]") -> List[int]:
        cur = node
        while cur and cur.prev:
            cur = cur.prev
        ans = []
        while cur:
            ans.append(cur.val)
            cur = cur.next
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
// Definition for a Node.
class Node {
    public int val;
    public Node prev;
    public Node next;
};
*/

class Solution {
    public int[] toArray(Node node) {
        var cur = node;
        while (cur != null && cur.prev != null) {
            cur = cur.prev;
        }
        var ans = new ArrayList<Integer>();
        while (cur != null) {
            ans.add(cur.val);
            cur = cur.next;
        }
        return ans.stream().mapToInt(i -> i).toArray();
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 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* node) {
        Node* cur = node;
        while (cur && cur->prev) {
            cur = cur->prev;
        }
        vector<int> ans;
        while (cur) {
            ans.push_back(cur->val);
            cur = cur->next;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Next *Node
 *     Prev *Node
 * }
 */

func toArray(node *Node) (ans []int) {
    cur := node
    for cur != nil && cur.Prev != nil {
        cur = cur.Prev
    }
    for cur != nil {
        ans = append(ans, cur.Val)
        cur = cur.Next
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 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(node: _Node | null): number[] {
    let cur = node;
    while (cur && cur.prev) {
        cur = cur.prev;
    }
    const ans: number[] = [];
    while (cur) {
        ans.push(cur.val);
        cur = cur.next;
    }
    return ans;
}

评论