LeetCode92. ๅ่ฝฌ้“พ่กจ II๐ŸŒŸ๐ŸŒŸ๐ŸŒŸไธญ็ญ‰

่ฏพๅŽไฝœไธš

้—ฎ้ข˜ๆ่ฟฐ

ๅŽŸๆ–‡้“พๆŽฅ๏ผš92. ๅ่ฝฌ้“พ่กจ II
็ป™ไฝ ๅ•้“พ่กจ็š„ๅคดๆŒ‡้’ˆ head ๅ’Œไธคไธชๆ•ดๆ•ฐ left ๅ’Œ right ๏ผŒๅ…ถไธญ left <= right ใ€‚่ฏทไฝ ๅ่ฝฌไปŽไฝ็ฝฎ left ๅˆฐไฝ็ฝฎ right ็š„้“พ่กจ่Š‚็‚น๏ผŒ่ฟ”ๅ›žๅ่ฝฌๅŽ็š„้“พ่กจ ใ€‚

็คบไพ‹ 1๏ผš

img

่พ“ๅ…ฅ๏ผšhead = [1,2,3,4,5], left = 2, right = 4
่พ“ๅ‡บ๏ผš[1,4,3,2,5]

็คบไพ‹ 2๏ผš

่พ“ๅ…ฅ๏ผšhead = [5], left = 1, right = 1
่พ“ๅ‡บ๏ผš[5]

ๆ็คบ๏ผš

  • ้“พ่กจไธญ่Š‚็‚นๆ•ฐ็›ฎไธบ n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

ไปฃ็ ๅฎž็Žฐ

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {

    ListNode last = null;

    public ListNode reverseBetween(ListNode head, int left, int right) {
    //left = 1
        if(left == 1){
            return reverseThN(head, right);
        }
        //1~left - 1
        head.next = reverseBetween(head.next, left - 1, right - 1);
        return head;
    }

    // ๅ่ฝฌ็ฌฌ1๏ฝžn็š„่Š‚็‚น
    ListNode reverseThN(ListNode head, int n){
        if(n == 1){
            last = head.next;
            return head;
        }

        ListNode newHead = reverseThN(head.next, n - 1);
        head.next.next = head;
        head.next = last;
        return newHead;
    }
}

Python

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

    def reverseBetween(self, head, left, right):
        """
        :type head: ListNode
        :type left: int
        :type right: int
        :rtype: ListNode
        """
        if left == 1:
            return self.reverseThN(head, right)

        head.next = self.reverseBetween(head.next, left - 1, right - 1)
        return head

    def reverseThN(self, head, n):
        if n == 1:
            self.last = head.next
            return head

        newHead = self.reverseThN(head.next, n - 1)
        head.next.next = head
        head.next = self.last
        return newHead

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
private:
    ListNode* last = nullptr;

public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        if (left == 1) {
            return reverseThN(head, right);
        }
        head->next = reverseBetween(head->next, left - 1, right - 1);
        return head;
    }

    ListNode* reverseThN(ListNode* head, int n) {
        if (n == 1) {
            last = head->next;
            return head;
        }

        ListNode* newHead = reverseThN(head->next, n - 1);
        head->next->next = head;
        head->next = last;
        return newHead;
    }
};

Go

ๅ‘่กจ่ฏ„่ฎบ

ๅŽๆ‰่ƒฝ่ฏ„่ฎบ