当前位置:网站首页>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);
}
边栏推荐
- [monitoring deployment practice] display the charts of Prometheus and loki+promtail based on granfana
- Image fuzzy processing batch production fuzzy data set
- 数仓4.0笔记——用户行为数据采集四
- 3.1. Simplified supplement to DQL
- MySQL tree structure recursive query
- 数仓4.0笔记---用户行为数据生成
- Ffmpeg audio coding
- 软件测试1
- Data warehouse 4.0 notes - business data collection
- 链表相关面试题
猜你喜欢

UE4解决WebBrowser无法播放H.264的问题

11. Multithreading
![[pyrodiomics] the extracted image omics characteristic value is abnormal (many 0 and 1)](/img/73/628e0188e1e2c13e0963746759f691.png)
[pyrodiomics] the extracted image omics characteristic value is abnormal (many 0 and 1)

Upload pictures to qiniu cloud through the web call interface

Data warehouse 4.0 notes - user behavior data collection I

Charles抓包的使用步骤

11、多线程
![[deployment] cluster deployment and startup of presto-server-0.261.tar.gz](/img/37/1185b2321b003a7793c8c37891008c.png)
[deployment] cluster deployment and startup of presto-server-0.261.tar.gz

對.h5文件的迭代顯示,h5py數據操作

百变冰冰!使用飞桨的PaddleGAN实现妆容迁移
随机推荐
ninja文件语法学习
Accordion effect
Ten year structure five year Life-02 first job
MySQL卸载
Data warehouse 4.0 notes - data warehouse environment construction - Yan configuration
数仓4.0笔记——业务数据采集——Sqoop
Affichage itératif des fichiers.h5, opérations de données h5py
1. Know the database
Mysqldump batch export MySQL table creation statement
Upload pictures to qiniu cloud through the web call interface
[radiology] bugfix: when GLCM features: indexerror: arrays used as indexes must be of integer (or Boolean) type
Image fuzzy processing batch production fuzzy data set
Adding environment variables and templates to systemctl service
链栈
對.h5文件的迭代顯示,h5py數據操作
APP自动化测试工具-appium的安装及使用
MySQL事务
Data warehouse 4.0 notes - user behavior data collection II
Review of knowledge points
ninja启动过程