आसान 0 हल किए गए 0% स्वीकृति

Linked List Operations

Simulate a linked list: add at either end, remove from the front or by value, and reverse it.

आप क्या सीखेंगे

  • Add and remove nodes at both ends
  • Remove the first node with a given value
  • Reverse the order of a list

समस्या

अनुवाद दिखाएँ

A linked list stores values in nodes that point to the next node, so adding or removing at the front is cheap. Simulate one that starts empty.

Process the operations in order:

  • push_front x: add x at the front.
  • push_back x: add x at the back.
  • pop_front: remove the first value (ignored when the list is empty).
  • remove x: remove the first node whose value is x, if there is one.
  • reverse: reverse the order of the nodes.

Implement linked_list_operations(operations).

Input

operations: The operations.

Output

Return the values in the list from front to back.

सीमाएँ

0 ≤ number of operations ≤ 10,000

−10^9 ≤ x ≤ 10^9

उदाहरण

उदाहरण 1
इनपुट:
operations = ["push_back 2", "push_front 1", "push_back 3", "reverse"]

अपेक्षित आउटपुट:
expected = [3, 2, 1]
इनपुट:
std::vector<std::string> operations = {"push_back 2", "push_front 1", "push_back 3", "reverse"};

अपेक्षित आउटपुट:
std::vector<long long> expected = {3, 2, 1};
इनपुट:
string[] operations = new string[] {"push_back 2", "push_front 1", "push_back 3", "reverse"};

अपेक्षित आउटपुट:
long[] expected = new long[] {3, 2, 1};

व्याख्या

The list grows to 1 → 2 → 3, and reversing it gives [3, 2, 1].

उदाहरण 2
इनपुट:
operations = ["push_back 5", "push_back 7", "push_back 5", "remove 5", "pop_front"]

अपेक्षित आउटपुट:
expected = [5]
इनपुट:
std::vector<std::string> operations = {"push_back 5", "push_back 7", "push_back 5", "remove 5", "pop_front"};

अपेक्षित आउटपुट:
std::vector<long long> expected = {5};
इनपुट:
string[] operations = new string[] {"push_back 5", "push_back 7", "push_back 5", "remove 5", "pop_front"};

अपेक्षित आउटपुट:
long[] expected = new long[] {5};

व्याख्या

remove takes out only the first 5, leaving 7 → 5; pop_front then removes 7: [5].

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

A list type with cheap front and back inserts (std::list, LinkedList<T>, deque) does most of the work.

संकेत 2

remove deletes only the first match, and pop_front on an empty list does nothing.

संकेत 3

Reversing changes which end is the front for later pushes and pops.

अपेक्षित जटिलता

O(n²) time, O(n) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।