LeetCode||92. Reverse Linked List II
2018-01-24 11:05
369 查看
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given
return
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
交换链表上的元素
# 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
"""
dummy = ListNode(0)
dummy.next = head
cur = dummy
for i in range(1, m):
cur = cur.next
tail = cur.next
for i in range(m, n):
if tail is None:
break
tmp = tail.next
tail.next = tmp.next
tmp.next = cur.next
cur.next = tmp
return dummy.next
For example:
Given
1->2->3->4->5->NULL, m = 2 and n = 4,
return
1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
交换链表上的元素
# 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
"""
dummy = ListNode(0)
dummy.next = head
cur = dummy
for i in range(1, m):
cur = cur.next
tail = cur.next
for i in range(m, n):
if tail is None:
break
tmp = tail.next
tail.next = tmp.next
tmp.next = cur.next
cur.next = tmp
return dummy.next
相关文章推荐
- [leetcode] 92. Reverse Linked List II
- LeetCode - 92. Reverse Linked List II
- 【LeetCode】92. Reverse Linked List II 解法及注释
- Leetcode 92. Reverse Linked List II 翻转链表2 解题报告
- leetcode题解-92. Reverse Linked List II
- LeetCode 92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II (Medium) (cpp)
- [LeetCode] 92. Reverse Linked List II
- LeetCode 92. Reverse Linked List II 解题报告
- LeetCode 92. Reverse Linked List II ***** 虚拟头结点
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
- Leetcode 92. Reverse Linked List II
- leetcode92. Reverse Linked List II
- [LeetCode]92. Reverse Linked List II
- Leetcode 92. Reverse Linked List II
- leetcode之92. Reverse Linked List II(C++读错题版本,交换一个链表中指定的两个位置上的元素)
- 【leetcode】92. Reverse Linked List II
- leetcode-92. Reverse Linked List II
- [Leetcode]92. Reverse Linked List II
- LeetCode-92. Reverse Linked List II