leetcode笔记—翻转链表

1、翻转链表

 void reverseNodes(ListNode *start, ListNode *end) {   //翻转链表
     ListNode *second = start -> next;
     ListNode *first = start;   
     ListNode *temp;
     
 
     while(second != end) {
         temp = second -> next;
         second -> next = first;
         first = second;
         second = temp;
     }
 
     second -> next = first;
     
    }     //翻转后start指向最后一个节点

ListNode* revertList(ListNode* head){  //翻转链表
        if(head == NULL){  
            return NULL;  
        }  
          
        ListNode* p = head->next;  
        head->next = NULL;  
          
        while(p != NULL){  
            ListNode* tmp = p;  
            p = p->next;  
            tmp->next = head;  
            head = tmp;  
        }  
        return head;  
    }



2、链表相邻的k个节点翻转

void reverseNodes(ListNode *start, ListNode *end) {
     ListNode *second = start -> next;
     ListNode *first = start;
     ListNode *temp;
 
     while(second != end) {
         temp = second -> next;
         second -> next = first;
         first = second;
         second = temp;
     }
 
     second -> next = first;
 }
 
 ListNode *reverseKGroup(ListNode *head, int k) {
     ListNode *tempHead = head, *tempEnd = head;
     ListNode *result = head;
     ListNode *preEnd = head;
     ListNode *temp = NULL;
     bool flag = true;
     int count = 1;
 
     if(!head) {
         return NULL;
     }
 
     while(tempEnd -> next) {
         tempEnd = tempEnd -> next;
         count++;
 
         if(count == k) {
             temp = tempEnd -> next;
             if(flag) {
                 flag = false;
                 result = tempEnd; 
                 reverseNodes(tempHead, tempEnd);
                 tempHead -> next = temp;
                 count = 1;
             }  //翻转第一个k个节点;
             else {
                 preEnd -> next = tempEnd;
                 preEnd = tempHead;
                 reverseNodes(tempHead, tempEnd);
                 tempHead -> next = temp;
                 count = 1;
             }//翻转后面的节点
 
             if(temp) {
                 tempHead = temp;
                 tempEnd = temp;
             }
             else {
                 tempHead -> next = NULL;
                 return result;
             }
         }
     }
     return result;
 }
};

//以上程序是有问题的,不知道哪里错了,下面是个accepted

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode** re=&head;
        ListNode* pre=head;  
        ListNode* q=NULL;
        while(true)
        {
            int i=k;
            ListNode* temp=pre;
            while(temp!=NULL&&--i>0)
            {
                temp=temp->next;
            }     //取到K个节点的尾
            if(temp==NULL) return head;//如果没有K长度,返回
            i=k;
            while(i--)
            {    //翻转
                ListNode *p_next=pre->next;
                pre->next=q;
                q=pre;
                pre=p_next;
            }
            (*re)->next=pre;
            ListNode *t=*re;
            *re=q;
            re=&(t->next);
            q=NULL;
            
        }
        return head;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值