# Linked list related interview questions Chapter 2

Posted by edtlov on Wed, 09 Feb 2022 22:19:31 +0100

# preface

### 1. Title Description

2. If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there is no ring in the linked list.
3. Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list. Returns true if there are links in the linked list. Otherwise, false is returned.

### 2. Problem solving ideas

The code is as follows:

```/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
{
while(fast&&fast->next)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
{
return true;
}
}
return false;
}
```

### 1. Title Description

1. Given a linked list, return the first node from the linked list into the ring. If the linked list is acyclic, null is returned.
In order to represent the rings in a given linked list, we use the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there is no ring in the linked list. Note that pos is only used to identify the ring and is not passed to the function as a parameter.
2. Note: it is not allowed to modify the given linked list.

### 2. Problem solving ideas

The code is as follows:

```struct ListNode *detectCycle(struct ListNode *head)
{
while(fast&&fast->next)
{
fast=fast->next->next;
slow=slow->next;
if(slow==fast)
{
struct ListNode* meet=fast;
{
meet=meet->next;
}
return meet;
}
}
return NULL;
}
```

# 3, Complex linked list with random pointer

Complex linked list with random pointer

### 1. Title Description

1. Give you a linked list with a length of N. each node contains an additional random pointer random, which can point to any node or empty node in the linked list. Construct a deep copy of this linked list. The deep copy should consist of exactly n new nodes, in which the value of each new node is set to the value of its corresponding original node. The next pointer and random pointer of the new node should also point to the new node in the replication linked list, and these pointers in the original linked list and replication linked list can represent the same linked list state. The pointer in the copy linked list should not point to the node in the original linked list.
2. For example, if there are two nodes X and Y in the original linked list, where x.random -- > y. Then the corresponding two nodes X and Y in the copy linked list also have x.random -- > y.
4. A linked list composed of n nodes is used to represent the linked list in input / output. Each node is represented by a [val, random_index]:
5. Val: one represents node Integer of val. random_index: the node index pointed by the random pointer (range from 0 to n-1); null if it does not point to any node. Your code only accepts the head node of the original linked list as the incoming parameter.

### 2. Problem solving ideas

The code is as follows:

```struct Node* copyRandomList(struct Node* head)
{
{
return NULL;
}
//1. The copy node is hung behind the original node
while(cur)
{
struct Node* next=cur->next;
struct Node* copy=(struct Node*)malloc(sizeof(struct Node));
copy->val=cur->val;
cur->next=copy;
copy->next=next;
cur=next;
}
//2. Handle the random of the copy node
while(cur)
{
struct Node* copy=cur->next;
if(cur->random==NULL)
{
copy->random=NULL;
}
else
{
copy->random=cur->random->next;
}
cur=copy->next;
}
//3. Take down the copied nodes and link them together to restore the original linked list
while(cur)
{
struct Node* copy=cur->next;
struct Node* next=copy->next;
//Tail insertion
copyTail->next=copy;
copyTail=copyTail->next;

cur->next=next;
cur=next;
}
free(guard);
}
```

# 4, Insert and sort the linked list

Insert and sort the linked list

### 1. Title Description

1. Insert and sort the linked list.
The animation of insert sort is shown above. Starting from the first element, the linked list can be considered to have been partially sorted (represented by black).
At each iteration, an element (indicated in red) is removed from the input data and inserted in place into the ordered linked list.
2. Insert sorting algorithm:
Insertion sorting is iterative, moving only one element at a time until all elements can form an ordered output list.
In each iteration, insert sort only removes an element to be sorted from the input data, finds its appropriate position in the sequence, and inserts it.
Repeat until all input data is inserted.

### 2. Problem solving ideas

The code is as follows:

```struct ListNode* insertionSortList(struct ListNode* head)
{
{
}
//Starting condition
while(cur)  //Termination condition cur is null
{
struct ListNode* next=cur->next;
struct ListNode* p=NULL;
while(c)
{
if(c->val>cur->val)
{
break;
}
else
{
p=c;
c=c->next;
}
}
if(p==NULL)  //Not a single node
{
cur->next=c;
}
else        //Middle insertion
{
p->next=cur;
cur->next=c;
}
cur=next;
}
}
```

# 5, Delete duplicate nodes in the linked list. Duplicate nodes are not retained

Delete duplicate nodes in the linked list. Duplicate nodes are not retained

### 1. Title Description

1. In a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes are not retained and the chain header pointer is returned. For example, the linked list 1 - > 2 - > 3 - > 3 - > 4 - > 4 - > 5 is 1 - > 2 - > 5 after processing

### 2. Problem solving ideas

The code is as follows:

```/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
{
{
}
struct ListNode* prev=NULL;
while(next)
{
if(cur->val==next->val)
{
while(next && cur->val==next->val)
{
next=next->next;
}
while(cur!=next)
{
struct ListNode* del=cur;
cur=cur->next;
free(del);
}
if(prev==NULL)
{
}
else
{
prev->next=cur;
}
if(next)
{
next=next->next;
}
}
else
{
prev=cur;
cur=next;
next=next->next;
}
}