算法学习

算法题--单向链表局部翻转

2020-04-26  本文已影响0人  岁月如歌2020
image.png

0. 链接

题目链接

1. 题目

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

2. 思路1:双指针法

left_last开始位置在head之左,
left_last=ListNode(-1), 
left_last.next=head, 
left=head, right=head.next

然后从左到右遍历, 记录遍历过的元素数 count

关于返回值,分两种情况

3. 代码

# coding:utf8


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


class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        left_last = ListNode(-1)
        left_last.next = head
        left = head
        right = head.next
        count = 0
        while True:
            count += 1
            if count < m:
                left_last = left
                left = left.next
                right = right.next
            elif count < n:
                temp = right.next
                right.next = left
                left = right
                right = temp
            else:
                left_last.next.next = right
                left_last.next = left
                break

        return left_last.next if m == 1 else head


def array_to_linked_list(array):
    head = node = ListNode(-1)
    for each in array:
        node.next = ListNode(each)
        node = node.next
    return head.next


def linked_list_to_array(head):
    array = list()
    while head is not None:
        array.append(head.val)
        head = head.next
    return array


def print_linked_list(head):
    print('->'.join([str(x) for x in linked_list_to_array(head)]))


def my_test(solution, array, m, n):
    head = array_to_linked_list(array)
    print('input: ')
    print_linked_list(head)
    print('m = {}, n = {}'.format(m, n))
    print('output: ')
    print_linked_list(solution.reverseBetween(head, m, n))
    print('=' * 50)
    print('')


solution = Solution()
my_test(solution, [1, 2, 3, 4, 5], 1, 3)
my_test(solution, [3, 5], 1, 2)

输出结果

input: 
1->2->3->4->5
m = 1, n = 3
output: 
3->2->1->4->5
==================================================

input: 
3->5
m = 1, n = 2
output: 
5->3
==================================================

4. 结果

image.png
上一篇 下一篇

猜你喜欢

热点阅读