博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 42: Reverse Nodes in k-Group
阅读量:6111 次
发布时间:2019-06-21

本文共 2763 字,大约阅读时间需要 9 分钟。

Feb 16 '12

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

 

 

/** * 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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if( !head || k<=1) return head;                ListNode ** p = &head;        ListNode * cur = head;        ListNode * pre = NULL;        ListNode * end;                while(true) {            int i=k;            // probe first            ListNode * temp = cur;            while( temp!=NULL && --i>0) {                temp = temp->next;            }            if(!temp) return head;                        i=k;            while(i-->0) {                end = cur->next;                cur->next = pre;                pre = cur;                cur = end;            }            (*p)->next = cur;            ListNode * t = *p;             *p = pre;            p = &(t->next);            pre = NULL;        }        return head;    }};

 

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode reverseKGroup(ListNode head, int k) {        // Start typing your Java solution below        // DO NOT write main() function        if(head==null || k==1) return head;                ListNode start = new ListNode(0);        start.next = head;        ListNode pre = null;        ListNode cur = head;        ListNode next = null;        ListNode end = null;        head = start;                while( cur != null) {                      int i=k;            end = cur;            while(--i>0 && end != null ){                end = end.next;            }            if(end==null) return head.next;                         i=k;            while(i-->0) {                next = cur.next;                cur.next = pre;                pre = cur;                cur = next;            }            start.next.next = cur;            ListNode t = start.next;            start.next = pre;            start = t;        }                  return head.next;    }}

转载于:https://www.cnblogs.com/xishibean/archive/2013/01/17/2951380.html

你可能感兴趣的文章
Excel到R中的日期转换
查看>>
检查磁盘利用率并且定期发送告警邮件
查看>>
MWeb 1.4 新功能介绍二:静态博客功能增强
查看>>
linux文本模式和文本替换功能
查看>>
Windows SFTP 的安装
查看>>
摄像机与绕任意轴旋转
查看>>
rsync 服务器配置过程
查看>>
预处理、const与sizeof相关面试题
查看>>
爬虫豆瓣top250项目-开发文档
查看>>
Elasticsearch增删改查
查看>>
oracle归档日志增长过快处理方法
查看>>
有趣的数学书籍
查看>>
teamviewer 卸载干净
查看>>
多线程设计模式
查看>>
解读自定义UICollectionViewLayout--感动了我自己
查看>>
SqlServer作业指定目标服务器
查看>>
UnrealEngine4.5 BluePrint初始化中遇到编译警告的解决办法
查看>>
User implements HttpSessionBindingListener
查看>>
抽象工厂方法
查看>>
ubuntu apt-get 安装 lnmp
查看>>