# Concatenation of two Linked Lists in O(1) time

In this tutorial, we will explore about the concatenation of two Linked lists in O(1) time.

## What exactly is a Linked List?

A linked list is a form of linear data structure wherein members will not be kept in sequential memory locations. A linked list's elements are linked via pointers, as demonstrated in the graphic below:

• It is the most popular type of linked list.
• In a singly linked list, each node holds some data and a pointer pointing to the next node.
• We must move in one way, from head to tail.

• It is a type of linked list which can move in both ways, head to tail and tail to head.
• A Doubly Linked List (DLL) has an additional pointer, sometimes referred to as the previous pointer, as well as the next pointer and data.

• Circularly Linked List is a kind of linked list in where all nodes are linked to create a circular form.
• The initial and last nodes in a circular linked list are connected to each other, forming a circle.
• So, at the end, there is no NULL.

### 1. For Singly and Doubly Linked List:

Concatenation of two lists in O(1) time with a doubly linked list or a singly linked list , given that at least each of the lists has a pointer to the last node. (There also exists some links which connects to the list head.)

Let's examine how it works in the example of Singly LL, but it also works in doubly.

C++ Program:

Output:

```First Linked List:
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
```

Java Program:

Output:

```First Linked List:
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
```

Time Complexity will be O(1).

Auxiliary Space will be O(1).

### 2. For Circular Linked List:

We may simply concatenate these in O(1) time using a circularly linked list. It's simply a question of breaking a link in both lists and then connecting them. This, of course, presupposes that the order of the things is unimportant.

Implementation:

C++ Program:

Output:

```Before Concatenation, First circular linked list: 1 2 3 4 5
Before Concatenation, Second circular linked list: 6 7 8 9 10
After Concatenation, First circular linked list: 1 7 8 9 10 6 2 3 4 5
After Concatenation, Second circular linked list: 6 2 3 4 5 1 7 8 9 10
```

Python Program:

Output:

```Before Concatenation, First circular linked list: 1 2 3 4 5
Before Concatenation, Second circular linked list: 6 7 8 9 10
After Concatenation, First circular linked list: 1 7 8 9 10 6 2 3 4 5
After Concatenation, Second circular linked list: 6 2 3 4 5 1 7 8 9 10
```

Time Complexity will be O(1).

Auxiliary Space will be O(1).