当前位置:网站首页>Linked list related interview questions
Linked list related interview questions
2022-07-23 11:58:00 【Look, that's a licking dog】
Structure
typedef int T;
typedef struct Node {
T data;
Node* next;
}*node;
typedef struct Node *LinkList;
1. The inverse of a single linked list
Node* Reverse(Node *Head)// The inverse of a single linked list
{
Node *p, *q, *r;
p = Head;
q = NULL;
r = NULL;
while (p)
{
q = p->next;
p->next = r;
r = p;
p = q;
}
return r;
}
2. Judge whether there is a ring
bool IsCircleList1(LinkList L)
{
LinkList p = L;
LinkList q = L;
while (p->next != NULL&&q->next != NULL)
{
p = p->next;
q = q->next->next;
if (p == q&&q != NULL)
return true;
if (q == NULL&&p != NULL)
return false;
}
return false;
}
bool IsCircleList2(LinkList L)
{
LinkList p = L;
LinkList q = L;
while (p != NULL&&q != NULL)
{
p = p->next;
q = q->next->next;
if (p == q)
break;
}
if (p == q&&q != NULL)
return true;
return false;
}
3. Judge the length of the ring
int GetCircleLen(LinkList L)
{
LinkList p = L;
LinkList q = L;
while (p != NULL&&q != NULL)
{
p = p->next;
q = q->next->next;
if (p == q)
break;
}
int count = 1;
while (p != q->next)
{
q = q->next;
count++;
}
return count;
}
4. Judge the position of the ring
LinkList GetCirclePos(LinkList L)
{
LinkList p, q;
p = L;
q = L;
while (p != NULL&&q != NULL)
{
p = p->next;
q = q->next->next;
if (p == q)
break;
}
if (p == q&&p != NULL&&p->next != NULL)
{
q = L;
while (p != q)
{
p = p->next;
q = q->next;
}
return p;
}
else return NULL;
}
5. Judge 2 Whether the two single linked lists intersect
bool IsInterseList(LinkList L1, LinkList L2)
{
LinkList p, q;
p = L1;
q = L2;
if (p == NULL || q == NULL)
return false;
while (p->next != NULL)
p = p->next;
while (q->next != NULL)
q = q->next;
if (p == q)
return true;
return false;
}
6. Determine whether the two tables intersect , Find the first point of intersection
int ListLength(LinkList L)// Chain length
{
int i = 0;
LinkList p = L->next;
while (p)
{
i++;
p = p->next;
}
return i;
}
LinkList GetIntersePos(LinkList L1, LinkList L2)
{
if (!(IsInterseList(L1, L2)))
return NULL;
int len1 = ListLength(L1);
int len2 = ListLength(L2);
LinkList p, q;
int step = len1 - len2;
if (step < 0)
{
step = 0 - step;
q = L1;
p = L2;
}
else{
p = L1;
q = L2;
}
for (int i = 0; i < step; i++)
{
p = p->next;
}
while (p != q)
{
p = p->next;
q = q->next;
}
return p;
}
7. Only one node in the single linked list is given p( Not the last node , namely p->next!=NULL) The pointer , Delete the node .
void deleteNode(LinkList del)
{
LinkList p;
p = del->next;
del->data = p->data;
del->next = p->next;
free(p);
}
8. Only one node in the single linked list is given p( Non empty node ), stay p Insert a node in front .
void interNode(Node* p, T val)
{
Node* newnode = (Node*)malloc(sizeof(Node));
newnode->next = p->next;
p->next = newnode;
newnode->data = p->data;
p->data = val;
}
9.// Look for the last K Nodes
LinkList FindBottomNode(LinkList L, int k)
{
if (L == NULL || k == 0)
return NULL;
LinkList head = L;
LinkList behind = L;
for (int i = 0; i < k - 1; i++)
{
if (head->next == NULL)
return NULL;
head = head->next;
}
while (head->next != NULL)
{
head = head->next;
behind = behind->next;
}
return behind;
}
10. Given the single chain header node , Delete the last... In the list k Nodes .
void deleteNode(LinkList L,int k)
{
if (L == NULL || k == 0)
return ;
LinkList head = L;
LinkList behind = L;
LinkList ptr = NULL;
for (int i = 0; i < k - 1; i++)
{
if (head->next == NULL)
return;
head = head->next;
}
while (head->next != NULL)
{
head = head->next;
behind = behind->next;
}
ptr = behind->next;
behind->data = ptr->data;
behind = ptr->next;
free(behind);
}
边栏推荐
- Accordion effect
- SQL realizes the user statistics of continuous login for more than 7 days
- ADB common commands
- Data warehouse 4.0 notes - user behavior data collection II
- Mosaic the face part of the picture
- BST树
- Es operation command
- 2. MySQL data management - DML (add, modify, delete data)
- [hudi]hudi compilation and simple use of Hudi & spark and Hudi & Flink
- NFT digital collection development: Jingdong "Qida bear takes you to the capital" tourism package
猜你喜欢

Entrepôt de données 4.0 Notes - acquisition de données commerciales

1、MySQL初体验
![[flick]flick on yarn's flick conf simplest configuration](/img/de/0ec23f3379148dba27fe77dc51e22f.png)
[flick]flick on yarn's flick conf simplest configuration

使用飞桨的paddleX-yoloV3对钢材缺陷检测开发和部署

Introduction to the process structure used by activiti workflow

Cuda10.0 configuration pytorch1.7.0+monai0.9.0

NFT digital collection system development: Xu Beihong Art Museum unveiled through the digital collection platform

Project instances used by activiti workflow

Charles抓包的使用步骤

Quartz2.2 simple scheduling job
随机推荐
二叉树
Iterative display of.H5 files, h5py data operation
Ten year structure five year Life-02 first job
MySQL transaction
Review of knowledge points
Wordcount of the first Flink program
One of scala variables and data types
互联网通信
Ten year structure five year life-01 at the beginning of graduation
Chinese interpretation of notepad++ background color adjustment options
1.认识数据库
MySQL user management
10、I/O 输入输出流
Data warehouse 4.0 notes - user behavior data collection I
Object类
DBA命令
Data warehouse 4.0 notes - business data collection - sqoop
Adding environment variables and templates to systemctl service
Lecturer solicitation order | Apache dolphin scheduler meetup sharing guests, looking forward to your topic and voice!
数仓4.0笔记——用户行为数据采集一