您的位置:首页 > 编程语言 > Python开发

[LeetCode]题解(python):092-Reverse Linked List II

2015-12-30 14:59 573 查看
[b]题目来源:[/b]

  https://leetcode.com/problems/reverse-linked-list-ii/

[b]题意分析:[/b]

  跟定一个链表,和位置m,n。将m到n的节点翻转。

[b]题目思路:[/b]

  和前面全部翻转的类似。

[b]代码(Python):[/b]

  

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if head == None or head.next == None:
return head
ans = ListNode(-1)
t,ans.next = ans,head
for i in range(m - 1):
t = t.next
tmp = t.next
for i in range(n- m):
tmp1 = t.next
t.next = tmp.next
tmp.next = tmp.next.next
t.next.next = tmp1
return ans.next


View Code

转载请注明出处:/article/6365032.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: