简单的链表面试题
1.逆置单链表
解题思路:将原链表进行头删,并对新链表进行头插。
源代码:
ListNode *reverseList(ListNode *head)
{
if (head == NULL)
return NULL;
struct ListNode *new_head = NULL;
struct ListNode *cur = NULL;
//头删和头插
while (head != NULL)
{
//头删
cur = head;
head = head->next;
//头插
cur->next = new_head;
new_head = cur;
}
return new_head;
}
2.合并两个有序链表
解题思路:给两个链表分别在定义一个指针,一步一步比较;
当某一个链表为空后,直接将非空链表直接接在重新连接的链表后面。
源代码:
ListNode *mergeOrderedList(ListNode *l1, ListNode *l2)
{
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
struct ListNode *curA = l1;
struct ListNode *curB = l2;
struct ListNode *curN = NULL;
struct ListNode *new_head = NULL;
//比较cur1和cur2
//将更小的放进new_head进行尾插
//如果两个值一样先放curA的
while (curA != NULL && curB != NULL)
{
if (curA->value <= curB->value)
{
if (curN == NULL)
{
curN = curA;
new_head = curN;
}
else
{
curN->next = curA;
curN = curN->next;
}
curA = curA->next;
}
else//curB的值小于curA的值
{
if (curN == NULL)
{
curN = curB;
new_head = curN;
}
else
{
curN->next = curB;
curN = curN->next;
}
curB = curB->next;
}
}
if (curA == NULL)
curN->next = curB;
else if (curB == NULL)
{
curN->next = curA;
}
return new_head;
}
3.以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
解题思路:将原链表拆成两部分,一部分为小于X的结点,另一部分为大于等于X的结点,最后根据所有特殊
条件,将这两个链表进行连接即可。
源代码:
ListNode* partition(ListNode* pHead, int x) {
// write code here
if (pHead == NULL)
return NULL;
struct ListNode *curLT = pHead;
struct ListNode *curGT = pHead;
struct ListNode *cur = pHead;
struct ListNode *gTHead = NULL;
struct ListNode *new_head = NULL;
while (cur)
{
if (cur->val < x)
{
if (new_head == NULL)
{
new_head = cur;
curLT = cur;
}
else
{
curLT->next = cur;
curLT = cur;
}
cur = cur->next;
}
else
{
if (gTHead == NULL)
{
gTHead = cur;
curGT = cur;
}
else
{
curGT->next = cur;
curGT = cur;
}
cur = cur->next;
}
}
if (gTHead == NULL)
curLT->next = NULL;
else if (new_head == NULL)
{
new_head = gTHead;
curGT->next = NULL;
}
else
{
curGT->next = NULL;
curLT->next = gTHead;
}
return new_head;
}
4.删除重复结点
解题思路:定义三个指针:prev、p1、p2;prve = NULL, p1 = pHead, p2 = pHead->next。如果p1 == p2,让p1不动p2向后走到不等于p1为止。然后释放掉p1到p2之前的结点,再让p1 = p2,p2向后走,prev要保证每次在p1的前面。
源代码:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL) {
return NULL;
}
ListNode *prev = NULL; // 用于删除的结点,是 p1 的前一个结点
ListNode *p1 = pHead;
ListNode *p2 = pHead->next;
ListNode *result = pHead; // 用于返回的第一个结点
while (p2 != NULL) {
if (p1->val != p2->val) {
prev = p1;
p1 = p2;
p2 = p2->next;
}
else {
while (p2 != NULL && p2->val == p1->val) {
p2 = p2->next;
}
// 删除
ListNode *next;
for (ListNode *node = p1; node != p2; node = next) {
next = node->next;
free(node);
}
// 重新拼接链表
if (prev != NULL) {
prev->next = p2;
}
else {
// 1 --> 1 --> 1 --> 2 --> NULL
// 的情况
result = p2;
}
p1 = p2;
if (p2 != NULL) {
p2 = p2->next;
}
}
}
return result;
}
5.删除链表中等于给定值val的所有结点
解题思路:当遍历到等于val的结点的时候,释放掉,向后走即可。
源代码:
struct ListNode* removeElements(struct ListNode* head, int val) {
if (head == NULL)
return NULL;
struct ListNode *cur = head;
struct ListNode *prev = head;
struct ListNode *next = NULL;
while (cur != NULL)
{
//当头结点是要删除的结点时
if (head->val == val)
{
//如果这个链表只有一个结点且是要删除的结点时
if (head->next == NULL)
{
free(head);
head = NULL;
return NULL;
}
next = head->next;
free(head);
head = next;
cur = head;
continue;
}
else if (cur->val == val)
{
next = cur->next;
free(cur);
prev->next = next;
cur = prev;
}
prev = cur;
if (cur != NULL)
cur = cur->next;
}
return head;
}
6.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
解题思路:定义两个指针,一个每次走一步,一个每次走两步。如果走的快的得那个指针,下一个结点或当前已经是空,那么走的慢的指针指的就是中间结点。
源代码:
struct ListNode* middleNode(struct ListNode* head) {
if (head == NULL)
return NULL;
struct ListNode *prev = head;
struct ListNode *cur = head;
while (cur != NULL)
{
if (prev->next == NULL)
return prev;
prev = prev->next;
cur = cur->next->next;
if (cur == NULL || cur->next == NULL)
return prev;
}
return prev;
}
7.输入一个链表,输出该链表中倒数第k个结点
解题思路:先遍历一下这个链表并求出该链表共有多少个结点;然后走总长 - k步,就到了倒数第K个结点。
源代码:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if (pListHead == NULL)
return NULL;
ListNode *cur = pListHead;
unsigned int count = 0;
//统计一共有多少个结点
while (cur != NULL)
{
count++;
cur = cur->next;
}
if (k > count)
return NULL;
cur = pListHead;
while (count - k)
{
cur = cur->next;
count--;
}
return cur;
}
8.链表的回文结构
解题思路:用栈结构,先将一半的结点入栈,然后如果该链表有奇数个结点,那么就让当前结点再往后走一个,因为最中间的那一个结点跟回不回文没关系。然后判断下一半的结点。
因为用到了栈,所以用C++比较方便
源代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (head == nullptr)
return true;
std::stack<int> s;
ListNode *cur = head;
int count = 0;
while (cur)
{
count++;
cur = cur->next;
}
cur = head;
for (int i = 0; i < count / 2 && cur; i++)
{
s.push(cur->val);
cur = cur->next;
}
if (count & 1)
cur = cur->next;
while (cur)
{
if (s.top() == cur->val)
{
s.pop();
cur = cur->next;
}
else
break;
}
if (s.empty())
return true;
return false;
}
};
9.相交链表
解题思路:分别求出两条链表的长度,然后让长的链表先走相差步,让两条链表当前一样长,然后两个链表一步一步比较,如果相等那么就是相交结点,如果遍历结束后还没有相交,那么说明两条链表没有相交。
源代码:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
//先遍历求出两条链表长度
int countA = 0;
int countB = 0;
struct ListNode *curA = headA;
struct ListNode *curB = headB;
while (curA)
{
countA++;
curA = curA->next;
}
while (curB)
{
countB++;
curB = curB->next;
}
curA = headA;
curB = headB;
int differ = 0;
if (countA > countB)
{
differ = countA - countB;
//先让长的走相差的步数
while (differ)
{
curA = curA->next;
differ--;
}
}
else
{
differ = countB - countA;
while (differ)
{
curB = curB->next;
differ--;
}
}
//现在两个链表一样长,比较每一个结点
while (curA && curB)
{
if (curA == curB)
return curA;
curA = curA->next;
curB = curB->next;
}
return NULL;
}
10.给定一个链表,判断链表中是否有环。
解题思路:定义两个指针,一个指针每次走两步,一个链表每次走一步,如果两个链表除第一次外再次相等,说明有环,否则没有环。
源代码:
bool hasCycle(struct ListNode *head) {
if (head == NULL)
return false;
struct ListNode *quick = head;
struct ListNode *slow = head;
while (quick && slow)
{
quick = quick->next;
if (quick == slow)
return true;
if (quick != NULL)
quick = quick->next;
else //如果quick == NULL
return false;
if (quick == slow)
return true;
slow = slow->next;
if (quick == slow)
return true;
}
return false;
}
11.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
解题思路:先判断是否有环,如果有环,将相遇的结点开始拆开,然后用相交链表的思路,求相交的结点,即为入环的第一个结点。
源代码:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
//先遍历求出两条链表长度
int countA = 0;
int countB = 0;
struct ListNode *curA = headA;
struct ListNode *curB = headB;
while (curA)
{
countA++;
curA = curA->next;
}
while (curB)
{
countB++;
curB = curB->next;
}
curA = headA;
curB = headB;
int differ = 0;
if (countA > countB)
{
differ = countA - countB;
//先让长的走相差的步数
while (differ)
{
curA = curA->next;
differ--;
}
}
else
{
differ = countB - countA;
while (differ)
{
curB = curB->next;
differ--;
}
}
//现在两个链表一样长,比较每一个结点
while (curA && curB)
{
if (curA == curB)
return curA;
curA = curA->next;
curB = curB->next;
}
return NULL;
}
struct ListNode *detectCycle(struct ListNode *head) {
if (head == NULL)
return NULL;
struct ListNode *quick = head;
struct ListNode *slow = head;
int index = 0;
//先求是否有环
while (quick && slow)
{
quick = quick->next;
if (quick == slow)
{
index = 1;
break;
}
if (quick != NULL)
quick = quick->next;
else //如果quick == NULL
if (quick == slow)
{
index = 1;
break;
}
slow = slow->next;
if (quick == slow)
{
index = 1;
break;
}
}
if (index == 1)
{
if (quick != NULL)
quick = quick->next;
if (slow != NULL)
slow->next = NULL;
struct ListNode *book = quick;
struct ListNode *Node = getIntersectionNode(head, quick);
if (slow != NULL)
slow->next = book;
return Node;
}
//没有环的情况
return NULL;
}
12.给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝
解题思路:先复制除random指针外的所有元素,然后将每个复制的结点,连接在它复制的结点后面;
接下来复制random,random为前一个结点的random->next。
最后将两个链表拆开。
源代码:
struct RandomListNode *copyRandomList(struct RandomListNode *head) {
if (!head)
return NULL;
//创建一个等长链表,复制除随机指针外的值,并将新创建的链表的每个结点连接在原链表的每个结点后
struct RandomListNode *cur = head;
while (cur != NULL)
{
struct RandomListNode *node = (struct RandomListNode *)malloc(sizeof(struct RandomListNode));
node->label = cur->label;
node->random = NULL;
//将结点连接在原结点后
node->next = cur->next;
cur->next = node;
cur = node->next;
}
cur = head;
//复制random结点
while (cur != NULL)
{
struct RandomListNode *next = cur->next;
if (cur->random != NULL)
next->random = cur->random->next;
cur = next->next;
}
cur = head;
//拆开两个链表
struct RandomListNode *new = head->next;
while (cur != NULL)
{
struct RandomListNode *next = cur->next;
cur->next = next->next;
if (next->next != NULL)
next->next = cur->next->next;
cur = cur->next;
}
return new;
}
总结:这些是一些简单的链表OJ题,我希望可以一步一步的增加自己的能力。不骄不躁,做好自己!