Leetcode - Reverse Nodes in k-Group

Screenshot from 2016-01-28 17:11:44.png

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k <= 1)
            return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode preTail = dummy;
        ListNode tail = head;
        ListNode tempHead = head;
        int counter = 0;
        while (tail != null) {
            counter++;
            if (counter >= k) {
                 ListNode temp = tail.next;
                /** reverse this part of list */
                tail.next = null;
                reverse(tempHead);
                tempHead.next = temp;
                preTail.next = tail;
                preTail = tempHead;
                tempHead= temp;
                tail = temp;
                counter = 0;
            }
            else {
                tail = tail.next;
            }
        }
        return dummy.next;
    }
    /** reverse the whole linked list */
    private void reverse(ListNode head) {
        if (head.next == null)
            return;
        ListNode next = head.next;
        ListNode pre = head;
        while (next != null) {
            ListNode temp = next;
            next = next.next;
            temp.next = pre;
            pre = temp;
        }
        head.next = null;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        ListNode head = test.reverseKGroup(n1, 2);
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }
}

這道題木自己做了出來,雖然期間用eclipse debug了一次,找出了錯誤。
思路是對的。設計一個計數(shù)器。然后走到固定點。
保存后面結點,切斷它與后面結點的聯(lián)系,然后reverse
然后和后面結點在接起來。繼續(xù)遍歷。
煩的是。下一次,當我翻轉下一個鏈表時,得到的新頭結點,需要更新,緊跟在上一個鏈表的尾結點之后。所以需要 preTail
還有要記錄下每一個子鏈表的頭, 所以需要 tempHead

這道題木難度還好,就是煩一些。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode curr = head;
        int counter = 0;
        while (curr != null) {
            counter++;
            if (counter < k) {
                curr = curr.next;
            }
            else {
                ListNode next = curr.next;
                ListNode tempHead = pre.next;
                curr.next = null;
                pre.next = reverse(tempHead);
                tempHead.next = next;
                pre = tempHead;
                curr = next;
                counter = 0;
            }
        }
        
        return dummy.next;
    }
    
    private ListNode reverse(ListNode head) {
        ListNode pre = head;
        ListNode curr = pre.next;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        head.next = null;
        return pre;
    }
}

并不是很難。

Anyway, Good luck, Richardo! -- 09/22/2016

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容