
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* reverseKGroup(ListNode* head, int k) {
// write code here
if(head==nullptr||head->next==nullptr||k<2)
return head;
ListNode* ft=new ListNode(-1);
ft->next=head;
int cd=0;
ListNode* pre=ft;
ListNode* now=head;
ListNode* nex=nullptr;
while(now!=nullptr)
{
cd++;
now=now->next;
}
now=head;
for(int i=1; i<=cd/k; i++)
{
for(int j=1; j<k; j++)
{
nex=now->next;
now->next=nex->next;
nex->next=pre->next;
pre->next=nex;
}
if(i==1)
ft->next=pre->next;
pre=now;
now=now->next;
}
return ft->next;
}
};

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
ListNode* addInList(ListNode* head1, ListNode* head2) {
// write code here
if(head1==nullptr)
return head2;
if(head2==nullptr)
return head1;
stack<ListNode*>s1;
stack<ListNode*>s2;
ListNode* p1=head1;
ListNode* p2=head2;
while(p1!=nullptr){
s1.push(p1);
p1=p1->next;
}
while(p2!=nullptr){
s2.push(p2);
p2=p2->next;
}
int sum=0,now=0;
while(!s1.empty()||!s2.empty()){
sum=now;
if(!s1.empty()){
sum+=s1.top()->val;
head1=s1.top();
s1.pop();
}
if(!s2.empty()){
sum+=s2.top()->val;
if(s2.size()>s1.size())
head1=s2.top();
s2.pop();
}
if(sum<10){
now=0;
head1->val=sum;
}
else{
now=sum/10;
head1->val=sum%10;
}
}
if(now>0){
head2=new ListNode(now);
head2->next=head1;
return head2;
}
return head1;
}
};