
题目一:113 · 删除排序链表中的重复数字(二) - LintCode

class Solution {
public:
ListNode* deleteDuplicates(ListNode *head) {
ListNode* node=new ListNode(-1);
node->next=head;
ListNode* pre=node;
ListNode* cur=node->next;
while(cur)
{
bool flag=false;
while(cur->next&&cur->val==cur->next->val)
{
flag=true;
cur=cur->next;
}
if(flag)
{
pre->next=cur->next;
cur=cur->next;
}
else
{
pre=cur;
cur=cur->next;
}
}
return node->next;
}
};
题目二:36 · 翻转链表(二) - LintCode

class Solution {
public:
ListNode* reverseBetween(ListNode *head, int m, int n) {
if(m==n||head==NULL)
return head;
int cnt=1;
ListNode* ls1=head,ls2=head;
while(cnt<m-1)
{
ls1=ls1->next;
ls2=ls2->next;
cnt++;
}
while(cnt<n-1)
{
ls2=ls2->next;
cnt++;
}
ListNode* next1,res;
ListNode* next2=ls2->next->next;
if(m==1)
{
next1=ls1;
res=reverse(ls1,ls2->next);
head=res;
while(res!=next1)
{
res=res->next;
}
res->next=next2;
}
else
{
next1=ls1->next;
res=reverse(ls1->next,ls2->next);
ls1->next=res;
while(res!=next1)
{
res=res->next;
}
res->next=next2;
}
return head;
}
ListNode* reverse(ListNode* ltnd1,ListNode* ltnd2)
{
ListNode* tmp=NULL;
while(ltnd1!=ltnd2)
{
ListNode* newnode=ltnd1;
ListNode* next=ltnd1->next;
newnode->next=tmp;
tmp=newnode;
ltnd1=next;
}
ListNode* newnode=ltnd1;
newnode->next=tmp;
tmp=newnode;
return tmp;
}
};
class Solution {
public:
ListNode* reverseBetween(ListNode *head, int m, int n) {
if(m==n||head==NULL)
return head;
vector<int> nums;
int index=1,i=0;
ListNode* cur=head;
while(cur)
{
if(index>=m&&index<=n)
{
nums.push_back(cur->val);
}
index++;
cur=cur->next;
}
index=1;
cur=head;
while(cur)
{
if(index>=m&&index<=n)
{
cur->val=nums[nums.size()-1-i];
i++;
}
index++;
cur=cur->next;
}
return head;
}
};
题目三:96 · 链表划分 - LintCode

class Solution {
public:
ListNode* partition(ListNode *head, int x) {
ListNode* leftnum=new ListNode(0);
ListNode* rightnum=new ListNode(0);
ListNode* left=leftnum;
ListNode* right=rightnum;
while(head)
{
if(head->val<x)
{
left->next=head;
left=head;
}
else
{
right->next=head;
right=head;
}
head=head->next;
}
left->next=rightnum->next;
right->next=NULL;
return leftnum->next;
}
};
题目四:LintCode 炼码
class Solution {
public:
ListNode* sortList(ListNode *head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* fast=head;
ListNode* slow=head;
while(fast->next&&fast->next->next)
{
fast=fast->next;
fast=fast->next;
slow=slow->next;
}
ListNode* mid=slow->next;
slow->next=NULL;
ListNode* ls1=sortList(head);
ListNode* ls2=sortList(mid);
ListNode* res=merge(ls1,ls2);
return res;
}
ListNode* merge(ListNode*list1,ListNode* list2)
{
if(list1==NULL) return list2;
if(list2==NULL) return list1;
ListNode* Head;
ListNode* cur;
if(list1->val<list2->val)
{
Head=list1;
list1=list1->next;
}else{
Head=list2;
list2=list2->next;
}
cur=Head;
while(list1&&list2)
{
if(list1->val<list2->val)
{
cur->next=list1;
cur=cur->next;
list1=list1->next;
}else
{
cur->next=list2;
cur=cur->next;
list2=list2->next;
}
}
if(list1) cur->next=list1;
if(list2) cur->next=list2;
return Head;
}
};
题目五:LintCode 炼码

class Solution {
public:
void reorderList(ListNode *head) {
if(head==NULL) return ;
vector<ListNode*> nodes;
ListNode* cur=head;
while(cur)
{
nodes.push_back(cur);
cur=cur->next;
}
int left=0;
int right=nodes.size()-1;
cur=head;
while(left<right)
{
nodes[left]->next=nodes[right];
nodes[right--]->next=nodes[++left];
}
nodes[left]->next=NULL;
}
};
题目六:LintCode 炼码

class Solution {
public:
bool hasCycle(ListNode * head) {
if(head==NULL) return false;
ListNode* fast=head;
ListNode* slow=head;
while(fast->next&&fast->next->next)
{
fast=fast->next;
fast=fast->next;
slow=slow->next;
if(fast==slow)
{
return true;
}
}
return false;
}
};
题目7:170 · 旋转链表 - LintCode

class Solution {
public:
ListNode* rotateRight(ListNode *head, int k) {
if(head==NULL||k==0) return head;
int Len=Listlen(head);
int step=k%Len;
ListNode* cur=head;
vector<ListNode*> nodes;
while(cur)
{
nodes.push_back(cur);
cur=cur->next;
}
cur=head;
ListNode* tmp1=reverse(cur,nodes[nodes.size()-1-step]);
ListNode* tmp2=reverse(nodes[nodes.size()-step],nodes[nodes.size()-1])
(nodes[0])->next=tmp2;
ListNode* res=reverse(nodes[nodes.size()-1-step],nodes[nodes.size()-step]);
return res;
}
ListNode* reverse(ListNode* list1,ListNode* list2)
{
ListNode* Head=NULL;
ListNode* tmp=
while(list1!=list2)
{
ListNode* newcode=list1;
newcode->next=Head;
Head=newcode;
list1=list1->next;
}
ListNode* newcode=list1;
newcode->next=Head;
Head=newcode;
return Head;
}
int Listlen(ListNode* head)
{
int res=0;
while(head)
{
res++;
head=head->next;
}
return res;
}
};
题目八:104 · 合并k个排序链表 - LintCode

struct compare {
bool operator()(const ListNode* l, const ListNode* r) {
return l->val > r->val;
}
};
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.size()==0) return NULL;
priority_queue<ListNode* , vector<ListNode *>, compare> pq;
for(auto i:lists)
{
if(i) pq.push(i);
}
ListNode * res = new ListNode(-1);
ListNode * tail =res;
while(!pq.empty())
{
ListNode* newnode=pq.top();
pq.pop();
tail->next=newnode;
tail=newnode;
if(newnode->next)
{
pq.push(newnode->next);
}
}
return res->next;
}
};