题目来源:

  https://leetcode.com/problems/reverse-nodes-in-k-group/


题意分析:

  这道题目和上一题目类似,输入一个链表和一个整型k。每k个翻转一下。不能更改链表的值。


题目思路:

  这道题目为了更加直观,先写一个翻转链表的函数。接下来就是链表操作。


代码(python):

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def reverse(self,start,end):
  8. nhead = ListNode(0)
  9. nhead.next = start
  10. while nhead.next != end:
  11. tmp = start.next
  12. start.next = tmp.next
  13. tmp.next = nhead.next
  14. nhead.next = tmp
  15. return [end,start]
  16. def reverseKGroup(self, head, k):
  17. """
  18. :type head: ListNode
  19. :type k: int
  20. :rtype: ListNode
  21. """
  22. ans = ListNode(0)
  23. if head == None:
  24. return None
  25. ans.next = head
  26. start = ans
  27. while start.next != None:
  28. end = start
  29. i = 0
  30. while i < k - 1:
  31. end = end.next
  32. if end.next == None:
  33. return ans.next
  34. i += 1
  35. tmp = self.reverse(start.next,end.next)
  36. start.next = tmp[0]
  37. start = tmp[1]
  38. return ans.next

转载请注明出处:http://www.cnblogs.com/chruny/p/4872990.html

[LeetCode]题解(python):025-Reverse Nodes in k-Group的更多相关文章

  1. [Leetcode] Reverse nodes in k group 每k个一组反转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  2. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

  3. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  4. LeetCode 025 Reverse Nodes in k-Group

    题目描述:Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time an ...

  5. LeetCode 笔记系列六 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. ...

  6. 【LeetCode】025. 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. k  ...

  7. Java for LeetCode 025 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 ...

  8. 025 Reverse Nodes in k-Group 每k个一组翻转链表

    给出一个链表,一次翻转 k 个指针节点,并返回修改后的链表.k 是一个正整数,并且小于等于链表的长度.如果指针节点的数量不是 k 的整数倍,那么最后剩余的节点应当保持原来的样子.你不应该改变节点的值, ...

  9. leetcode第24题--Reverse Nodes in k-Group

    problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...

  10. LeetCode之“链表”:Reverse Nodes in k-Group

    题目链接 题目要求: Given a linked list, reverse the nodes of a linked list k at a time and return its modifi ...

随机推荐

  1. POJ 1037 DP

    题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...

  2. [置顶] ios 在一定选项范围随机选取选项demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9408285 demo功能:ios 在一定范围随机选取demo,如截屏.在 ...

  3. BootStrap 智能表单系列 十 自动完成组件的支持

    web开发中,肯定遇到像百度.google这种搜索的功能吧,那智能表单中的自动完成可以做什么呢,下面来揭晓: 1.包含像google.百度等类似的简单搜索 2.复杂结构的支持,比如说 输入产品编号,需 ...

  4. JavaWEB HTTP请求中POST与GET的区别

    From 的get 和post方法.在数据传输过程中分别对应了HTTP协议中的GET和POST方法. 二者主要区别: GET从服务其获取数据;POST上传数据. GET将表单中的数据按照variabl ...

  5. BZOJ 1192 鬼谷子的钱袋

    题目如下 Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会 ...

  6. Python学习之路——初识Python

    一.第一个程序Hello World: 1.打印输出Hello World: Python2打印方法: >>> print "hello world"hello ...

  7. socket 通信 入门3 android 客户端 C# 服务端

    这是一个android端操控服务器的例子  就是发送简单指令到服务器  然后服务器响应什么的... 当然这里是未完成的  只是简单展示一下大致思路 首先连接建立起来后  服务端给客户端一条信息  告诉 ...

  8. json对象的封装与解析

    一.解析json对象 表结构信息对象,json格式,名称为tableObj   *  {   *   "tableName":"t_res",          ...

  9. 【LeetCode】Flatten Binary Tree to Linked List

    随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...

  10. poj 1836 Alignment(线性dp)

    题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...