题目地址:Reverse Linked List II - LeetCode
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
这道题目是我之前做一家公司笔试时候遇到的算法题,反转部分链表,难度比反转全部链表大。
当时做题的时候不会告诉你错误的样例,现在重新写,在特殊情况的判断中依然会有出错,以后需要重新再做。
Python解法如下:
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution: