有时候面试是ACM模式,需要自己实现链表的各种操作。习惯了力扣的核心代码模式,就不会手写链表。这里记录一下题目,您可以从这几道题中逐渐学会如何手写链表。
单链表排序
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node {
int val;
Node * next;
} a[N], *head;
int n;
int main()
{
scanf("%d", &n);
head = nullptr;
for (int i = 1; i <= n; i ++ ) // 在插入元素的同时就进行排序
{
scanf("%d", &a[i].val);
if (!head || a[i].val < head->val)
{
a[i].next = head;
head = &a[i];
}
else
{
for (auto p = head; p; p = p->next)
{
if (p->val < a[i].val && (!p->next || a[i].val < p->next->val))
{
a[i].next = p->next;
p->next = &a[i];
break;
}
}
}
}
for (auto p = head; p; p = p->next)
printf("%d ", p->val);
return 0;
}
合并链表
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node {
int val;
Node *next;
} a[N], b[N], *h1, *h2, *t1, *t2;
int n, m;
Node* merge()
{
Node *l1 = h1, *l2 = h2, *ans = nullptr, *t = nullptr;
while (l1 || l2)
{
if (l1 && (!l2 || l1->val < l2->val))
{
if (!ans) ans = t = l1;
else t = t->next = l1;
l1 = l1->next;
}
else
{
if (!ans) ans = t = l2;
else t = t->next = l2;
l2 = l2->next;
}
}
return ans;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) // 尾插法构建单链表h1
{
scanf("%d", &a[i].val);
if (!h1) h1 = t1 = &a[i];
else t1 = t1->next = &a[i];
}
scanf("%d", &m);
for (int i = 1; i <= m; i ++ ) // 尾插法构建单链表h2
{
scanf("%d", &b[i].val);
if (!h2) h2 = t2 = &b[i];
else t2 = t2->next = &b[i];
}
Node *head = merge();
for (Node *p = head; p; p = p->next)
printf("%d ", p->val);
return 0;
}
链表中间元素
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node {
int val;
Node *next;
} a[N], *head, *tail;
int n;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) // 尾插法构建单链表head
{
scanf("%d", &a[i].val);
if (!head) head = tail = &a[i];
else tail = tail->next = &a[i];
}
Node *slow = head, *fast = head; // 快慢指针
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
printf("%d\n", slow->val);
return 0;
}
上面的这段代码,对于序列[1,8,7,6],n=4,答案会输出元素7。那么如果想要严格输出第n/2个数,即输出元素8,那么代码如下:
Node *slow = head, *fast = head->next; // 快慢指针
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
反转链表
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node {
int val;
Node *next;
} a[N], *head, *tail;
int n;
Node* reverse()
{
Node *pre = nullptr, *cur = head;
while (cur)
{
auto nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &a[i].val);
if (!head) head = tail = &a[i];
else tail = tail->next = &a[i];
}
auto h = reverse();
for (auto p = h; p; p = p->next)
printf("%d ", p->val);
return 0;
}
双向循环链表
这里使用双向循环链表来求解约瑟夫问题。
输入:10 3
输出:3 6 9 2 7 1 8 5 10 4
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node {
int val;
Node *prev, *next;
} a[N], *head, *tail;
int n, m;
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
{
a[i].val = i;
if (!head) head = tail = &a[i];
else
{
tail->next = &a[i];
a[i].prev = tail;
tail = tail->next;
}
}
tail->next = head;
head->prev = tail;
auto p = tail;
int c = 0;
while (1)
{
p = p->next;
c ++ ;
if (c == m)
{
printf("%d ", p->val);
if (p == p->next) break;
auto x = p->prev, y = p->next;
x->next = y, y->prev = x;
p = x;
c = 0;
}
}
return 0;
}