链表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个节点不存在的情况. 参考代码: ...
随机推荐
- QT---线程间通信(要先编译libqt-mt.so?)
在 Qt 系统中,运行着一个GUI 主事件线程,这个主线程从窗口系统中获取事件,并将它们分发到各个组件去处理.在 QThread 类中有一种从非主事件线程中将事件提交给一个对象的方法,也就是 QThr ...
- C# 集合性能比较(代码测试)
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- java 下拉框级联及相关(转)
ActionLintsner都实现此接口,其它监听器可以监听的事件都可以被它捕获 public interface ActionListener extends EventListenerThe li ...
- 生成excel文件
java操作Excel最常用的开源组件有poi与jxl.jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007.poi是apache下的一个子项目,poi应该是处理ms的o ...
- UGUI Text控件
学习UGUI的Text控件,用于显示文本!. 基本属性就不再啰嗦了! Alignment: 文字以 水平和垂直 对齐方式, Horizontal Overflow: 水平 Wrap: 文字大小和数量 ...
- Unity position和localposition
1. position是根据世界原点为中心 2. localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的 3.选中一个物体左上角Globa ...
- linux下php扩展curl的安装
方法一 安装cURL wget http://curl.haxx.se/download/curl-7.17.1.tar.gz tar -zxf curl-7.17.1.tar.gz ./config ...
- mysql 5.5源码包安装
注:由于mysql5.5的源码包安装与mysql之前的版本安装方法不同,故写一篇随笔记录.5.5的版本不再是./configure make make install 这里用到了cmake了,cmak ...
- Android Studio Errors
1.The import org.apache.http.client; tip: cannot be resolved; resolve: add this line in build.gradle ...
- UIView的clipsTobounds属性
这里的clip是修剪的意思,bounds是边界的意思,合起来就是:如果子视图的范围超出了父视图的边界,那么超出的部分就会被裁剪掉.该属性在实际工程中还是非常实用的,必须要了解清楚.