链表k个节点反向
问题:
以k个元素为一组,反转单向链表。比如:
输入: 1->2->3->4->5->6->7->8->null and k = 3
输出:3->2->1->6->5->4->8->7->null.
分析:
我们可以把整个链表分成多个长度为 k 的子链表, 然后,我们再反转每一个子链表(递归)。问题的关键是我们需要把每个子链表再连接起来。所以,对每一个子链表操作以后,我们需要返回该子链表的头(head),然后,我们设置前一个子链表的最后一个node,把它的next 设置成下一个链表返回的头(head),这样,所有的子链表就连接起来了。
- public static Node reverse (Node head, int k) {
- Node current = head;
- Node next = null;
- Node prev = null;
- int count = 0;
- /*reverse first k nodes of the linked list */
- while (current != null && count < k) {
- next = current.next;
- current.next = prev;
- prev = current;
- current = next;
- count++;
- }
- /* next is now a pointer to (k+1)th node
- Recursively call for the list starting from current.
- And make rest of the list as next of first node */
- if(next != null) {
- head.next = reverse(next, k);
- }
- /* prev is new head of the input list */
- return prev;
- }
public static Node reverse (Node head, int k) {
Node current = head;
Node next = null;
Node prev = null;
int count = 0;
/*reverse first k nodes of the linked list */
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if(next != null) {
head.next = reverse(next, k);
}
/* prev is new head of the input list */
return prev;
}
这个问题也可以使用非递归的方法,基本上问题的处理方式和递归是一样的,但是,非递归的方式要稍微复杂一点,因为每次对子链表反转以后,我们需要更新前一个子链表最后一个node 的next 值。代码如下:
- class Node {
- int val;
- Node next;
- Node(int x) {
- val = x;
- next = null;
- }
- }
- public class Solution {
- public static void main(String[] args) {
- Solution s = new Solution();
- Node n1 = new Node(1);
- Node n2 = new Node(2);
- Node n3 = new Node(3);
- Node n4 = new Node(4);
- Node n5 = new Node(5);
- n1.next = n2;
- n2.next = n3;
- n3.next = n4;
- n4.next = n5;
- Node head = s.ReverseInGroups(n1, 2);
- while (head != null) {
- System.out.println(head.val);
- head = head.next;
- }
- }
- public Node ReverseInGroups(Node current, int k) {
- if (current == null || current.next == null ) return current;
- //store the new head of the list
- Node newHead = null;
- //store the last node in the sub-list,
- //we will update its next value when finishing
- //reversing the next sub-list
- Node previousGroupTail = null;
- int count = 1; //used to track the first sub-list
- while (current != null) {
- // the actual tail in the sub-list
- Node groupTail = current;
- //reverse
- Node prev = null;
- Node next = null;
- for (int i = 1; i <= k && current != null; i++) {
- next = current.next;
- current.next = prev;
- prev = current;
- current = next;
- }
- // get the new head of the whole list
- if (count == 1) {
- newHead = prev;
- count++;
- }
- // update the next value of the last node in
- // each sub-list
- if (previousGroupTail != null) {
- previousGroupTail.next = prev;
- }
- previousGroupTail = groupTail;
- }
- return newHead;
- }
- }
链表k个节点反向的更多相关文章
- [leetcode]61. Rotate List反转链表k个节点
类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...
- 剑指Offer面试题:14.链表的倒数第k个节点
PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...
- 链表之求链表倒数第k个节点
题目描述:输入一个单向链表,输出该链表中倒数第k个节点,链表的倒数第0个节点为链表的尾指针. 最普遍的方法是,先统计单链表中结点的个数,然后再找到第(n-k)个结点.注意链表为空,k为0,k为1,k大 ...
- LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表
题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...
- 剑指offer-第三章高质量的代码(输出该链表中倒数第K个节点)
题目:输入一个链表,输出这个链表中倒数第K个节点.(代码的鲁棒性) 思路:用两个指针p1和p2,都指向头节点,开始的时候,p2不动,p1移动k-1次,指向第k个节点.此时,如果p1->next! ...
- 链表的倒数第K个节点
题目:输入一个链表,输出该链表中倒数第K个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个结点. package com.edu; class LinkNode{ //定义一 ...
- 删除链表中全部值为k的节点
1. 问题描写叙述 给定一个单链表,删除当中值为k的全部节点.比如:1→2→6→3→4→5→61 \to 2 \to 6 \to 3 \to 4 \to 5 \to 6,删除当中值为6的节点,返回:1 ...
- 13. 求链表倒数第k个节点
题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...
- 链表中倒数第K个节点
问题描述: 找出链表中倒数第K个节点 思路分析: 用两个指针,一前一后,保持k个距离,前面的指针移动到末尾,后面的指针就刚好直到第k个节点, 要考虑到k为0,倒数第k个节点不存在的情况. 参考代码: ...
随机推荐
- ListView与CheckBox组合实现单选
main.xml配置文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Android ListView中带有时间数据的排序
下面是activity: public class MainActivity extends Activity { private ListView mListView = null; private ...
- xampp安装时mysql报错
问题描述:以前安装过mysql,后来安装xampp,mysql打不开,出错提示16:04:48 [mysql] MySQL Service detected with wrong path16:0 ...
- mongoDB windows reinstall add auth
Mongodb默认启动是不带认证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,这样可不行.现在,我们得一步步开启使用用户和认证. 第一步,我们得定位到mongodb的安装目录.我本机的是C ...
- 三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)
摘自http://blog.csdn.net/liujun13579/article/details/7772491 三十一.Java图形化界面设计--布局管理器之GridLayout(网格布局) 网 ...
- 【转】通过 ulimit 改善系统性能
概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...
- 多线程:pthread_exit,pthread_join,pthread_self
/*exit_join_id.c*/ #include<pthread.h> #include<stdio.h> void* eji(void* agr) { printf(& ...
- 【思路题】【多校第一场】【1001.OO’s Sequence】
题目大意: 给你一个序列A,f(l,r) 表示 在[l,r]中 的Ai 对于每一个数Aj 都有 Ai%Aj!=0 的数目( i!=j ) 卡了一段时间..... 题解 简单题 定义两个数组L[i ...
- Java - 文件(IO流)
Java - 文件 (IO) 流的分类: > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter ...
- 不同SQL Server数据库之间的跨数据库查询
--不同SQL Server数据库之间的跨数据库查询 EXEC sp_addlinkedserver @server=N'OldDatabase', --自己定义别名 @srvproduct=N'', ...