Leetcode:NO.25 K flip list

subject

Topic link: https://leetcode-cn.com/problems/reverse-nodes-in-k-group

Give you a list, each k nodes a group to flip, please return to the flipped list.

k is a positive integer whose value is less than or equal to the length of the list.

If the total number of nodes is not an integral multiple of k, keep the last remaining nodes in the same order.

Example:

Here is the list: 1 - > 2 - > 3 - > 4 - > 5

When k = 2, it should return: 2 - > 1 - > 4 - > 3 - > 5

When k = 3, it should return: 3 - > 2 - > 1 - > 4 - > 5
explain:

Your algorithm can only use constant extra space.
You can't just change the internal values of nodes, but you need to actually exchange nodes.

Problem solving record

  • Change the size group conversion of K
  • Record each group of head nodes and tail nodes
  • Loop inserts the value of the head node after the tail node
  • Traverse the linked list until the group with less than enough K size appears, and jump out
/**
 * @author ffzs
 * @describe K A set of flip list
 *
 * Give you a list, each k nodes a group to flip, please return to the flipped list.
 * k Is a positive integer whose value is less than or equal to the length of the linked list.
 *
 * If the total number of nodes is not an integral multiple of k, keep the last remaining nodes in the same order.

 * Example:
 *
 * Here is the list: 1 - > 2 - > 3 - > 4 - > 5
 * When k = 2, it should return: 2 - > 1 - > 4 - > 3 - > 5
 * When k = 3, it should return: 3 - > 2 - > 1 - > 4 - > 5
 *
 * explain:
 *
 * Your algorithm can only use constant extra space.
 * You can't just change the internal values of nodes, but you need to actually exchange nodes.
 *
 * @date 2020/5/16
 */
public class reverseKGroup {

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public static ListNode reverseKGroup (ListNode head, int k) {
        if (head==null) return null;
        ListNode res = new ListNode(-1);
        res.next = head;
        head = res;
        ListNode tail = res;
        while (true) {
            for (int i = 0; i < k && tail != null; i++) tail = tail.next;
            if (tail==null) break;
            ListNode pre = head.next;
            while (head.next != tail){
                ListNode tmp = head.next;
                head.next = tmp.next;
                tmp.next = tail.next;
                tail.next = tmp;
            }
            head = pre;
            tail = head;
        }
        return res.next;
    }


    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode tmp = head;
        int[] lst = {2,3,4,5};
        for (int i :lst){
            tmp.next = new ListNode(i);
            tmp = tmp.next;
        }
        System.out.println(reverseKGroup(head, 2));
    }
}

Advanced

Use recursion to solve

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public static ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) return null;
        ListNode cur = head;
        int count = 0;
        while (cur != null && count != k) {
            cur = cur.next;
            count++;
        }
        if (count == k) {
            cur = reverseKGroup(cur, k);
            while (count != 0) {
                count--;
                ListNode tmp = head.next;
                head.next = cur;
                cur = head;
                head = tmp;
            }
            head = cur;
        }
        return head;
    }
}

Tags: less

Posted on Sun, 17 May 2020 10:28:20 -0400 by velkymx