链表简介
链表是有序的列表,其特点如下:
1)链表是以节点的方式来存储,是链式存储
2)每个节点包含data域,next域:指向下一个节点.
3)链表的各个节点不一定是连续存储.
4)链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
带头结点的单链表,头节点的data域不存放具体的数据,作用就是表示单链表头;头节点的next域就是存放的是地址
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
// 在节点 a->b 中插入 c 插入操作
ListNode *p = a;
c->next = b;
p->next = c;
p = c; // 这个应该不需要的吧?
// 从链表 a->c->b 中删除节点 c
ListNode *p = c;
c = c->next;
a->next = c;
delete(p);
// 交换链表 a->c->b->d 中的 c 和 b 节点
a->next = b; // a 指向 b
c->next = b->next; // c 指向 d
b->next = c; // b 指向 c
单链表面试题
1)求单链表中有效节点的个数
//求单链表中有效节点个数
public int getLength(ListNode* head) {
if (head->next == null) {//空链表
return 0;
}
int length = 0;
//定义一个辅助的变量,这里我们没有统计头节点
ListNode* cur = head->next;
while (cur != null) {
length++;
cur = cur->next;
}
return length;
}
2)查找单链表中的倒数第k个结点
通过双指针的方法来处理,视频教学
class Solution {
public:
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
ListNode* fast= pHead;
ListNode* slow = pHead;
for(int i=0;i<k;i++)
{
if(!fast)
return 0;
fast = fast->next;
}
while(fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
3)单链表的反转
通过依次改变节点的转向来实现链表的反转,教学视频
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* pre = NULL;
ListNode* next = pHead->next;
if(!pHead || !pHead->next)
return pHead;
while(pHead)
{
next = pHead->next; // 保存下一个节点
pHead->next = pre; // 将当前节点指向上一个节点
pre = pHead; // 将当前节点上保存一个节点
pHead = next; // 移动到下一个节点
}
return pre;
}
};
4)合并两个排序的链表
通过向一个新建的节点加链表的节点,视频教学
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* dummy= new ListNode(0);
ListNode* cur;
cur = dummy;
while(pHead1 and pHead2)
{
if(pHead1->val <= pHead2->val)
{
cur->next = pHead1;
pHead1 = pHead1->next;
}
else{
cur->next = pHead2;
pHead2 = pHead2->next;
}
cur = cur->next;
}
if(pHead1!=NULL)
cur->next = pHead1;
if(pHead2!=NULL)
cur->next = pHead2;
return dummy->next;
}
};
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
if(!pHead1 || !pHead2)
return NULL;
ListNode* cur1 = pHead1;
ListNode* cur2 = pHead2;
int n = 0;
while(cur1->next)
{
n++;
cur1 = cur1->next;
}
while(cur2->next)
{
n--;
cur2 = cur2->next;
}
if(cur1 != cur2)
{
return nullptr;
}
if(n>0)
{
cur1 = pHead1;
cur2 = pHead2;
}
else
{
cur1 = pHead2;
cur2 = pHead1;
n = -n;
}
while(n)
{
n--;
cur1 = cur1->next;
}
while(cur1 != cur2)
{
cur1 = cur1->next;
cur2 = cur2->next;
}
return cur1;
}
};
6)判断一个链表是否为回文结构
回文结构的定义:正序倒序都是一样的,如121,1221,aba,abcdedcba都是回文数,视频教学
class Solution {
public:
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
ListNode* fast = head;
ListNode* slow = head;
while(fast->next and fast->next->next)
{
slow = slow->next;
fast = fast->next->next;
}
fast = slow->next;
slow->next = NULL;
ListNode* newList = fast;
while(fast)
{
newList = fast->next;
fast->next = slow;
slow = fast;
fast = newList;
}
newList = slow;
fast = head;
// 判断回文
while(fast != NULL and newList != NULL)
{
if(fast->val != newList->val)
{
return false;
}
fast = fast->next;
newList = newList->next;
}
return true;
}
};
class Solution {
public:
/**
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
if(!head)
return head;
ListNode* res= head;
ListNode* cur= res;
while(head)
{
if(cur->val == head->val)
{
head = head->next;
cur ->next = NULL;
}
else
{
cur->next = head;
cur = cur->next;
head = head->next;
}
}
return res;
}
};