博客
关于我
【牛客网-名企高频面试题】NC50 链表中的节点每k个一组翻转
阅读量:361 次
发布时间:2019-03-04

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

在这里插入图片描述

解题思路

在这里插入图片描述

public class Solution {       public static ListNode reverseKGroup(ListNode head, int k) {   		if(head == null || head.next == null || k < 2) return head;		ListNode dummy = new ListNode(0);		dummy.next = head;		ListNode pre = dummy, cur = head, temp;		int len = 0;		while (head != null) {   			len ++ ;			head = head.next;		}		for (int i = 0; i < len / k; i ++ ) {   			for (int j = 1; j < k; j ++ ) {   				temp = cur.next;				cur.next = temp.next;				temp.next = pre.next;				pre.next = temp;			}			pre = cur;			cur = cur.next;		}		return dummy.next;	}}

转载地址:http://gser.baihongyu.com/

你可能感兴趣的文章
MySQL高级-触发器
查看>>
mysql高级查询~分页查询
查看>>
MySQL高频面试题
查看>>
MySQL高频面试题的灵魂拷问
查看>>
MySQL(2)DDL详解
查看>>
Mysql,sql文件导入和导出
查看>>
MYSQL:int类型升级到bigint,对PHP开发语言影响
查看>>
Mysql:mysql 5.X 报错 ERROR 1193 (HY000): Unknown system variable ‘validate_password_length‘
查看>>
MySQL:MySQL执行一条SQL查询语句的执行过程
查看>>
Mysql:SQL性能分析
查看>>
mysql:SQL按时间查询方法总结
查看>>
MySQL:什么样的字段适合加索引?什么样的字段不适合加索引
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N-Gram的基本原理
查看>>
nacos config
查看>>