链表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个节点不存在的情况. 参考代码: ...
随机推荐
- FFmpeg深入分析之零-基础 <第一篇>
FFmpeg是相当强大的多媒体编解码框架,在深入分析其源代码之前必须要有基本的多媒体基础知识,否则其源代码会非常晦涩难懂.本文将从介绍一些基本的多媒体只是,主要是为研读ffmpeg源代码做准备,比如一 ...
- 【Xamarin挖墙脚系列:关闭 OS X El Capitan 中 SIP 安全设置功能】
比如需要修改内核配置文件: com.apple.Boot.plist 那么我们需要解锁权限. 禁止SIP模式,那么就可以修改此文件了. 在 OS X El Capitan 中有一个跟安全相关的模式叫 ...
- 蓝桥杯 六角形中填置1~12个数字 dfs
如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填写多余的内容. ...
- Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- 对中级Linux 用户非常有用的20 个命令
也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令的一个延伸. 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户.在这里你将学会如何进行自定 ...
- Objective C - UIColor
UIColor+Hex.h #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *) colorWithHexStrin ...
- 第五章 Logistic回归
第五章 Logistic回归 假设现在有一些数据点,我们利用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归. 为了实现Logistic回归分类器,我们可以在每个特征上都乘以一 ...
- XML.ObjTree -- XML source code from/to JavaScript object like E4X
转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...
- 在IIS上Office Word下载失败,检索 COM 类工厂中 CLSID 为000209FF的组件失败,80070005 拒绝访问。
最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现问题了. 出现问题如下:Error:下载简历方法出错:检索 COM 类工厂中 CLSID 为 {000 ...
- json 去空值与缩进
var jSetting = new Newtonsoft.Json.JsonSerializerSettings(); //忽略值为null的 jSetting.NullValueHandling ...