leetcode **25. K 個一組翻轉鏈表

【題目】**25. K 個一組翻轉鏈表

給你一個鏈表,每 k 個節點一組進行翻轉,請你返回翻轉後的鏈表。
k 是一個正整數,它的值小於或等於鏈表的長度。
如果節點總數不是 k 的整數倍,那麼請將最後剩餘的節點保持原有順序。

示例:
給你這個鏈表:1->2->3->4->5
當 k = 2 時,應當返回: 2->1->4->3->5
當 k = 3 時,應當返回: 3->2->1->4->5

說明:
你的算法只能使用常數的額外空間。
你不能只是單純的改變節點內部的值,而是需要實際進行節點交換。

【解題思路1】模擬

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        myReverseKGroup(head, k);
        return head;
    }
    public void myReverseKGroup(ListNode ln, int k) {
        if (ln == null) {
            return;
        }
        int[] temp = new int[k];
        int i = 0;
        ListNode ln_temp = ln;
        for (; i<k && ln != null; i++) {
            temp[i] = ln.val;
            ln = ln.next;
        }
        if (i == k) {
            for (int l=0; l<k; l++) {
                ln_temp.val = temp[k-1-l];
                ln_temp = ln_temp.next;
            }
        }
        myReverseKGroup(ln, k);
    }
}
//遞歸
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (k==1||head==null)
            return head;
        return helper(head,k-1);
    }

    public ListNode helper(ListNode head,int k){
        if (head==null)
            return null;
        ListNode start=head,end=start;
        int index;
        for (index=0;index<k;++index){//讓end移動到第k個節點上
            if (end==null)
                break;
            end=end.next;
        }
        if (index!=k||end==null)//這種情況下,說明不夠k個,直接返回head,即不翻轉
            return head;
        ListNode node=end==null?null:end.next;
        end.next=null;//這裏先將k個節點與後面的連接斷開,方便翻轉鏈表
        reverse(start);//翻轉這k個節點
        start.next=helper(node,k);//讓前k個節點的頭節點的next指向後k個節點的尾節點
        return end;
    }

    /**
     * 翻轉鏈表
     * @param head
     */
    public ListNode reverse(ListNode head){
        ListNode pre=head,next=pre.next;
        while (next!=null){
            ListNode tmp=next.next;
            next.next=pre;
            pre=next;
            next=tmp;
        }
        return head;
    }
}
//迭代
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (k==1||head==null)
            return head;
        return iterateHelper(head,k-1);
    }

    public ListNode iterateHelper(ListNode head,int k){
        ListNode start=head,res=null;
        ListNode pre=null;
        while (true){
            int index;
            ListNode end=start;
            for (index=0;index<k;++index){
                if (end==null)
                    break;
                end=end.next;
            }
            if (index!=k||end==null)
                break;
            ListNode node=end==null?null:end.next;
            end.next=null;
            reverse(start);
            if (pre!=null){
                pre.next=end;
            }else {
                res=end;
            }
            pre=start;
            start=node;
        }
        if (pre!=null)
            pre.next=start;
        return res;
    }

    /**
     * 翻轉鏈表
     * @param head
     */
    public ListNode reverse(ListNode head){
        ListNode pre=head,next=pre.next;
        while (next!=null){
            ListNode tmp=next.next;
            next.next=pre;
            pre=next;
            next=tmp;
        }
        return head;
    }
}

【解題思路2】


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章