Moderate algorithm | 36. Flip II list

Title Description

Flip linked list of node m to the node of the n-th part.

Sample 1

输入: 1->2->3->4->5->NULL, m = 2 and n = 4, 
输出: 1->4->3->2->5->NULL.

Sample 2

输入: 1->2->3->4->NULL, m = 2 and n = 3, 
输出: 1->3->2->4->NULL.

Problem-solving ideas

  1. Create a head.
  2. Find the previous node -Pre m
  3. Pre recorded a node, it will be tail reversing chain.
  4. Inverted list of the designated section, when a turn to the last node, the reverseTail.next directed to it next. This put the list after flipping the list and pick up.
  5. Returns the next node dummynode.

java solution to a problem

public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (m >= n || head == null) {
            return head;
        }
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        
        for (int i = 1; i < m; i++) {
            if (head == null) {
                return null;
            }
            head = head.next;
        }
        
        ListNode premNode = head;
        ListNode mNode = head.next;
        ListNode nNode = mNode, postnNode = mNode.next;
        for (int i = m; i < n; i++) {
            if (postnNode == null) {
                return null;
            }
            ListNode temp = postnNode.next;
            postnNode.next = nNode;
            nNode = postnNode;
            postnNode = temp;
        }
        mNode.next = postnNode;
        premNode.next = nNode;
        
        return dummy.next;
    }
}

C ++ solution to a problem

using namespace std;

class Solution {
public:
    void reverse(ListNode *head) {
        ListNode *prev = NULL;
        while (head != NULL) {
            ListNode *temp = head->next;
            head->next = prev;
            prev = head;
            head = temp;
        }
    }

    ListNode *findkth(ListNode *head, int k) {
        for (int i = 0; i < k; i++) {
            if (head == NULL) {
                return NULL;
            }
            head = head->next;
        }
        return head;
    }

    ListNode *reverseBetween(ListNode *head, int m, int n) {
        ListNode *dummy = new ListNode(-1, head);
        ListNode *mth_prev = findkth(dummy, m - 1);
        ListNode *mth = mth_prev->next;
        ListNode *nth = findkth(dummy, n);
        ListNode *nth_next = nth->next;
        nth->next = NULL;

        reverse(mth);
        mth_prev->next = nth;
        mth->next = nth_next;
        return dummy->next;
    }
};

python problem solution

from lintcode import ListNode

class Solution:
    def reverse(self, head):
        prev = None
        while head != None:
            next = head.next
            head.next = prev
            prev = head
            head = next
        return prev

    def findkth(self, head, k):
        for i in xrange(k):
            if head is None:
                return None
            head = head.next
        return head

    def reverseBetween(self, head, m, n):
        dummy = ListNode(-1, head)
        mth_prev = self.findkth(dummy, m - 1)
        mth = mth_prev.next
        nth = self.findkth(dummy, n)
        nth_next = nth.next
        nth.next = None

        self.reverse(mth)
        mth_prev.next = nth
        mth.next = nth_next
        return dummy.next
Published 164 original articles · won praise 23 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43233085/article/details/104104978