- 链表反转
ListNode* ReverseList(ListNode* head){
ListNode* a = NULL;
ListNode* b = head;
while(b!=NULL){
ListNode* c=b->next;
b->next=a;
a=b;
b=c;
}
return a;
}
- 链表内指定区间反转
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* res = new ListNode(-1);
res->next = head;
ListNode* pre = res;
ListNode* cur = head;
for(int i = 1; i < m; i++){
pre = cur;
cur = cur->next;
}
for(int i = m; i < n; i++){
ListNode* temp = cur->next;
cur->next = temp->next;
temp->next = pre->next;
pre->next = temp;
}
return res->next;
}
- 合并两个有序的链表
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
if(pHead1 ==NULL){
return pHead2;
}
if(pHead2 ==NULL){
return pHead1;
}
ListNode* head =new ListNode(0);
ListNode* cur=head;
while(pHead1&&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;
}
else cur->next=pHead2;
return head->next;
}
- 合并k个已排序的链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<int> v;
for(int i=0;i<lists.size();i++){
ListNode* temp=lists[i];
while(temp){
v.push_back(temp->val);
temp=temp->next;
}
}
sort(v.begin(),v.end());
ListNode* head=new ListNode(0);
ListNode* cur=head;
for(int i=0;i<v.size();i++){
ListNode* temp=new ListNode(v[i]);
cur->next=temp;
cur=temp;
}
return head->next;
}
};
- 判断链表中是否有环
bool hasCycle(ListNode *head) {
//先判断链表为空的情况
if(head==NULL)
return false;
//快慢双指针
ListNode* fast = head;
ListNode* slow = head;
//如果没环快指针会先到链表尾
while(fast != NULL&&fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
if(fast==slow)
return true;
}
return false;
}
- 链表中环的入口地址
ListNode* EntryNodeOfLoop(ListNode* head){
int N[10100]={0};
while(head){
if(!N[head->val])
N[head->val]=1;
else return head;
head=head->next;}
return NULL;
}
- 链表倒数第k个节点
ListNode* FindKthToTail(ListNode* pHead, int k) {
int len=0;
auto p=pHead;
while(p){
p=p->next;
len++;
}
if(len<k)
return NULL;
p=pHead;
for(int i=1;i<len-k+1;i++){
p=p->next;
}
return p;
}
8.两个链表的第一个公共节点
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
auto p=pHead1,q=pHead2;
while(p!=q){
if(p!=NULL) p=p->next;
else p=pHead2;
if(q!=NULL) q=q->next;
else q=pHead1;
}
return p;
}
9.单链表的排序
ListNode* sortInList(ListNode* head) {
vector<int> v;
ListNode* p=head;
while(p!=NULL){
v.push_back(p->val);
p=p->next;
}
p=head;
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
p->val=v[i];
p=p->next;
}
return head;
}
- 判断一个链表是否为回文结构
bool isPail(ListNode* head){
vector<int> nums;
while(head){
nums.push_back(head->val);
head=head->next;
}
vector<int> temp=nums;
reverse(temp.begin(),temp.end());
for(int i=0;i<nums.size();i++){
if(nums[i]!=temp[i]){
return false;
}
}
return true;
}
- 链表的奇偶重排
ListNode* oddEvenList(ListNode* head) {
if(head==NULL)
return head;
ListNode* even = head->next;
ListNode* odd = head;
ListNode* evenhead = even;
while(even!=NULL&&even->next!=NULL){
odd->next=even->next;
odd=odd->next;
even->next=odd->next;
even=even->next;
}
odd->next=evenhead;
return head;
}
- 有序链表中删除有序链表中重复的元素
ListNode* deleteDuplicates(ListNode* head) {
//空链表
if(head == NULL)
return NULL;
//遍历指针
ListNode* cur = head;
//指针当前和下一位不为空
while(cur != NULL && cur->next != NULL){
//如果当前与下一位相等则忽略下一位
if(cur->val == cur->next->val)
cur->next = cur->next->next;
//否则指针正常遍历
else
cur = cur->next;
}
return head;
}
- 有序链表中删掉出现不止一次的元素
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL){
return NULL;
}
unordered_map<int,int> mp;
ListNode* p=head;
while(p!=NULL){
mp[p->val]++;
p=p->next;
}
ListNode* res = new ListNode(0);
res->next=head;
p=res;
while(p->next!=NULL){
if(mp[p->next->val]!=1){
p->next=p->next->next;
}
else p=p->next;
}
return res->next;
}