new
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *cur1=l1,*cur2=l2;
ListNode *res=new ListNode(0);
ListNode *prev=res;
int t=0;
while(cur1||cur2||t)
{
if(cur1)
{
t+=cur1->val;
cur1=cur1->next;
}
if(cur2)
{
t+=cur2->val;
cur2=cur2->next;
}
prev->next=new ListNode(t%10);
prev=prev->next;
t/=10;
}
prev=res->next;
delete res;
return prev;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==nullptr || head->next==nullptr) return head;
ListNode *newhead=new ListNode(0);
newhead->next=head;
ListNode *prev=newhead,*cur=prev->next,*next=cur->next,*nnext=next->next;
while(cur&&next)
{
prev->next=next;
next->next=cur;
cur->next=nnext;
prev=cur;
cur=nnext;
if(cur) next=cur->next;
if(next) nnext=next->next;
}
cur=newhead->next;
delete newhead;
return cur;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
ListNode *slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
ListNode *head2=new ListNode(0);
ListNode *cur=slow->next;
slow->next=nullptr;
while(cur)
{
ListNode *next=cur->next;
cur->next=head2->next;
head2->next=cur;
cur=next;
}
ListNode *ans=new ListNode(0);
ListNode *anscur=ans,*cur1=head,*cur2=head2->next;
while(cur1)
{
anscur->next=cur1;
cur1=cur1->next;
anscur=anscur->next;
if(cur2)
{
anscur->next=cur2;
cur2=cur2->next;
anscur=anscur->next;
}
}
delete head2;
delete ans;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
struct cmp
{
bool operator()(ListNode* l1,ListNode *l2)
{
return l1->val > l2->val; //值较大的向下调整
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*,vector<ListNode*>,cmp> heap;
//所有的头结点进入小根堆中
for(auto l:lists)
{
if(l) heap.push(l);
}
//合并
ListNode *ans=new ListNode(0);
ListNode *prev=ans;
while(!heap.empty())
{
ListNode* t=heap.top();
heap.pop();
prev->next=t;
prev=t;
if(t->next) heap.push(t->next);
}
prev=ans->next;
delete ans;
return prev;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
int n=0;
ListNode *cur=head;
while(cur)
{
n++;
cur=cur->next;
}
n/=k;
ListNode *newhead=new ListNode(0);
cur=head;
ListNode *prev=newhead;
for(int i=0;i<n;i++)
{
ListNode *tmp=cur;
for(int j=0;j<k;j++)
{
ListNode *next=cur->next;
cur->next=prev->next;
prev->next=cur;
cur=next;
}
prev=tmp;
}
prev->next=cur;
cur=newhead->next;
delete newhead;
return cur;
}
};