LINTCODE——K组翻转链表
来源:互联网 发布:致知格物是什么意思 编辑:程序博客网 时间:2024/06/01 10:28
LINTCODE——K组翻转链表
思路:和翻转链表差不多,就是要注意每隔K个值打断、翻转、拼接就好了
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: /* * @param head: a ListNode * @param k: An integer * @return: a ListNode */ ListNode * reverseKGroup(ListNode * head, int k) { // write your code here ListNode *theadNode = new ListNode(0); theadNode -> next = head; ListNode *resNode = theadNode; ListNode *curNode = head; int cnt = 0; while(curNode != NULL) { cnt++; if(cnt == k) { //打断 ListNode *tcurNode = curNode; curNode = curNode -> next; tcurNode -> next = NULL; //翻转 head = reverse(head); //拼接 theadNode -> next = head; ListNode *tail = head; while(tail->next) tail = tail->next; tail -> next = curNode; //重新定义头节点 theadNode = tail; head = curNode; cnt = 0; continue; } curNode = curNode -> next; } return resNode -> next; } ListNode *reverse(ListNode *head) { if(head == NULL) return head; ListNode *curNode = head; ListNode *preNode = NULL; while(curNode != NULL) { ListNode *nextNode = curNode -> next; curNode -> next = preNode; preNode = curNode; curNode = nextNode; } return preNode; }};
阅读全文
0 0
- LINTCODE——K组翻转链表
- LintCode:K组翻转链表
- lintcode(450)K组翻转链表
- [LintCode]450.K组翻转链表
- K组翻转链表-LintCode
- lintcode——翻转链表
- LintCode-翻转链表
- lintcode 翻转链表
- LintCode:翻转链表
- lintcode,翻转链表
- 翻转链表,lintcode
- Lintcode 翻转链表
- LintCode 翻转链表
- 翻转链表-LintCode
- LintCode : 翻转链表
- [Lintcode]翻转链表
- lintcode--翻转链表
- Lintcode翻转链表
- 扩展欧几里得及逆元模板
- Kafka 设计与原理详解
- GKNoise
- 学习IT技术的十步曲
- 【NOIP】玩具谜题
- LINTCODE——K组翻转链表
- 错误的返回值
- ros中的move_base
- 【Qt】QT之中多个信号连接同一个槽【细说版】
- 「Git」合并多个 Commit
- iOS程序 防止动态调试
- mybatis简单案例源码详细【注释全面】——实体层(User.java)
- 欢迎使用CSDN-markdown编辑器
- 从零开始学习Linux部署Java web项目