[LeetCode]题解(python):025-Reverse Nodes in k-Group
题目来源:
https://leetcode.com/problems/reverse-nodes-in-k-group/
题意分析:
这道题目和上一题目类似,输入一个链表和一个整型k。每k个翻转一下。不能更改链表的值。
题目思路:
这道题目为了更加直观,先写一个翻转链表的函数。接下来就是链表操作。
代码(python):
- # Definition for singly-linked list.
- # class ListNode(object):
- # def __init__(self, x):
- # self.val = x
- # self.next = None
- class Solution(object):
- def reverse(self,start,end):
- nhead = ListNode(0)
- nhead.next = start
- while nhead.next != end:
- tmp = start.next
- start.next = tmp.next
- tmp.next = nhead.next
- nhead.next = tmp
- return [end,start]
- def reverseKGroup(self, head, k):
- """
- :type head: ListNode
- :type k: int
- :rtype: ListNode
- """
- ans = ListNode(0)
- if head == None:
- return None
- ans.next = head
- start = ans
- while start.next != None:
- end = start
- i = 0
- while i < k - 1:
- end = end.next
- if end.next == None:
- return ans.next
- i += 1
- tmp = self.reverse(start.next,end.next)
- start.next = tmp[0]
- start = tmp[1]
- return ans.next
转载请注明出处:http://www.cnblogs.com/chruny/p/4872990.html
[LeetCode]题解(python):025-Reverse Nodes in k-Group的更多相关文章
- [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 ...
- 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 算法思想:基本操作就是链表 ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 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 ...
- 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. ...
- 【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 ...
- 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 ...
- 025 Reverse Nodes in k-Group 每k个一组翻转链表
给出一个链表,一次翻转 k 个指针节点,并返回修改后的链表.k 是一个正整数,并且小于等于链表的长度.如果指针节点的数量不是 k 的整数倍,那么最后剩余的节点应当保持原来的样子.你不应该改变节点的值, ...
- 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 ...
- 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 ...
随机推荐
- POJ 1037 DP
题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...
- [置顶] ios 在一定选项范围随机选取选项demo
原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9408285 demo功能:ios 在一定范围随机选取demo,如截屏.在 ...
- BootStrap 智能表单系列 十 自动完成组件的支持
web开发中,肯定遇到像百度.google这种搜索的功能吧,那智能表单中的自动完成可以做什么呢,下面来揭晓: 1.包含像google.百度等类似的简单搜索 2.复杂结构的支持,比如说 输入产品编号,需 ...
- JavaWEB HTTP请求中POST与GET的区别
From 的get 和post方法.在数据传输过程中分别对应了HTTP协议中的GET和POST方法. 二者主要区别: GET从服务其获取数据;POST上传数据. GET将表单中的数据按照variabl ...
- BZOJ 1192 鬼谷子的钱袋
题目如下 Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会 ...
- Python学习之路——初识Python
一.第一个程序Hello World: 1.打印输出Hello World: Python2打印方法: >>> print "hello world"hello ...
- socket 通信 入门3 android 客户端 C# 服务端
这是一个android端操控服务器的例子 就是发送简单指令到服务器 然后服务器响应什么的... 当然这里是未完成的 只是简单展示一下大致思路 首先连接建立起来后 服务端给客户端一条信息 告诉 ...
- json对象的封装与解析
一.解析json对象 表结构信息对象,json格式,名称为tableObj * { * "tableName":"t_res", ...
- 【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 ...
- poj 1836 Alignment(线性dp)
题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...