每日算法之二十三:Reverse Nodes in k-Group

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

跟链表的成对转换有异曲同工之意。这里主要考虑的有两点:

1)链表逻辑转换的调用和操作

2)不要存在悬浮指针和断开链接

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reverse(ListNode * first,ListNode * end)//把k个元素置逆
    {
         ListNode * p1 = first;
        ListNode * p2;
		while(p1 != end&&p1!=NULL)//避免出现悬浮指针,也就是不要对空指针操作
        {
            p2 = p1->next;
            p1->next = end->next;
            end->next = p1;
	    p1 = p2;
        }//最后first指针在最后了,指向了后续链表
    }
    ListNode *reverseKGroup(ListNode *head, int k) {
       if(head == NULL || k<2)
       return head;
       ListNode * first = head;
       ListNode * end = first->next;
	   ListNode * pre = NULL;
       int i = 0;
       while(i<k-2&&end!=NULL)//end要指向第k个节点
       {
           end = end->next;
           i++;
        }
       while(NULL != end)
       {
            if(first == head)//只有一次
                head = end;
	   else pre->next = end;
            reverse(first,end);
			pre = first;
            first = first->next;
			if(first == NULL)
				break;
			end = first->next;
            i =0;
            while(i<k-2&&end!=NULL)
            {
                end = end->next;
                i++;
            }
       }
       return head;
    }
};

每日算法之二十三:Reverse Nodes in k-Group,布布扣,bubuko.com

时间: 2024-12-26 11:10:09

每日算法之二十三:Reverse Nodes in k-Group的相关文章

每日算法之二十二:Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

每日算法之二十八:Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

每日算法之二十五:Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 不使用乘法.除法和求模运算求两个数相除. class Solution { public: long long internalDivide(unsigned long long dividend,unsigned long long divisor) { if(dividend<divisor) return 0; int result =

每日算法之二十六:Substring with Concatenation of All Words

变相的字符串匹配 给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置: 1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次. 2)在出现的过程中不能出现其他的干扰单词. 3)出现的位置可能有多个. 4)单词的出现顺序不做要求. 下面是一个例子: S:"barfoothefoobarman" L:"foo","bar" 位置0是出现位置,:两个单词均出现仅出现一次,且没有干扰.同样位置9也

每日算法之二十九:Search in Rotated Sorted Array

在一个经过旋转后的有序数组中查找一个目标元素. Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1.

每日算法之二十:Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 给出数字n,求

【leetcode每日一题】25.Reverse Nodes in k-Group

题目: 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

JAVA常见算法题(二十三)

package com.xiaowu.demo; /** * 给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. * * * @author WQ * */ public class Demo23 { public static void main(String[] args) { f(123789); } public static void f(long l) { String s = Long.toString(l); char[] c = s.toCharArray(

算法系列之二十三:离散傅立叶变换之音频播放与频谱显示

算法系列之二十三:离散傅立叶变换之音频播放与频谱显示 算法系列之二十三离散傅立叶变换之音频播放与频谱显示 导语 什么是频谱 1 频谱的原理 2 频谱的选择 3 频谱的计算 显示动态频谱 1 实现方法 2 杂项说明 结果展示 导语 频谱和均衡器,几乎是媒体播放程序的必备物件,没有这两个功能的媒体播放程序会被认为不够专业,现在主流的播放器都具备这两个功能,foobar 2000的十八段均衡器就曾经让很多人着迷.在上一篇对离散傅立叶变换介绍的基础上,本篇就进一步介绍一下频谱是怎么回事儿,下一篇继续介绍