How to combine two linked lists O (1) times in c?

Given the two lists l1, l2, show how to combine them O (1) times. The data structures for lists depend on how you design it. Joining together, I want to say combining lists.

For example: List1 = {1,2,3} List2 = {2,4,5}

Combined List = {1,2,3,4,5}

+3
source share
4 answers

What you are looking for is not an algorithm for combining two “lists” in O (1) time. If you read “lists” as “linked lists”, this cannot be done faster than O (n).

, , - , O (1) . . "" O (1), , O (1). , Fibonacci Heap.

+9

""

O(1).

, , "" , O(1), O(N) ( N - ).

, , - , , , . O(1) ( ), .


O(1)

, O(1), , . set presentation, .

, O(α(n)) . α Ackermann; , , α . O(1) .

, "disjoint": {1, 2, 3} {2, 4, 5}, . ( ), .

< href= "http://en.wikipedia.org/wiki/Bit_array" rel= "noreferrer" > -, , 0/1 / . ; . , .

+11

, , , , - .

Will this work? Since you have two link lists, on the last element of the first list is the first element of the second list? Are we still talking about pointers right? the pointer to the last element of the list now points to the first element of the second list.

It works?

Edit: but we are looking for a union. so I guess it won't be ...

0
source

You are using the fact that the PC is a state machine with 2 ^ (bits of memory / memory space) and thereby declares everything O(1).

-1
source

Source: https://habr.com/ru/post/1759775/


All Articles