Leetcode - Rotate List

Paste_Image.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 rotateRight(ListNode head, int k) {
        if (head == null || k < 0)
            return head;
        else if (head.next == null)
            return head;
        
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode tail = head;
        int total = 1;
        while (tail.next != null) {
            tail = tail.next;
            total++;
        }
        k = k % total;
        if (k == 0)
            return dummy.next;
        
        ListNode curr = dummy;
        for (int i = 0; i < (total - k); i++)
            curr = curr.next;
        if (curr.next == null)
            return dummy.next;
        ListNode newHead = curr.next;
        curr.next = null;
        tail.next = dummy.next;
        dummy.next = newHead;
        return dummy.next;
    }
    
    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);
        ListNode n5 = new ListNode(5);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        System.out.println(test.rotateRight(n1, 3));
    }
}

My test result:

Paste_Image.png

這次題目也不難。先遍歷一遍找出總個(gè)數(shù) total
然后根據(jù) total - k 找到那個(gè)斷裂點(diǎn)。斷開(kāi)。在用dummy和新的頭接上。
原來(lái)的頭接到tail后面去,就差不多了。
主要我是沒(méi)能理解, k >= total 是什么意義。
查了之后發(fā)現(xiàn),做一個(gè) k = k % total 處理就行了。

**
總結(jié): LinkedList rotate
**

Anyway, Good luck, Richardo!

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null)
            return null;
        int n = 1;
        ListNode temp = head;
        ListNode tail = head;
        while (temp.next != null) {
            n++;
            temp = temp.next;
            tail = temp;
        }
        k = k % n;
        if (k == 0)
            return head;
        temp = head;
        for (int i = 1; i < n - k; i++) {
            temp = temp.next;
        }
        ListNode newHead = temp.next;
        temp.next = null;
        tail.next = head;
        return newHead;
    }
    
    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        Solution test = new Solution();
        ListNode head = test.rotateRight(n1, 2);
        ListNode temp = head;
        while (temp != null) {
            System.out.print(temp.val + "->");
            temp = temp.next;
        }
    }
}

有一個(gè) corner case沒(méi)有考慮到,當(dāng)k= 0 時(shí),這個(gè)時(shí)候是不需要旋轉(zhuǎn)的,如果強(qiáng)制旋轉(zhuǎn), newHead = tail.next -> null. 會(huì)出錯(cuò)。

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 rotateRight(ListNode head, int k) {
        if (k < 0 || head == null) {
            return null;
        }
        
        ListNode tail = head;
        int total = 0;
        while (tail.next != null) {
            total++;
            tail = tail.next;
        }
        total++;
        k = k % total;
        if (k == 0) {
            return head;
        }
        int counter = total - k - 1;
        ListNode curr = head;
        while (counter > 0) {
            curr = curr.next;
            counter--;
        }
        
        ListNode newHead = curr.next;
        curr.next = null;
        tail.next = head;
        return newHead;
    }
}

k = 0 的時(shí)候,不需要rotate,直接返回就行。
Anyway, Good luck, Richardo! -- 08/16/2016

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

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評(píng)論 0 33
  • My code: My test result: 這道題目不難吧。主要一個(gè)問(wèn)題,你能否in place的完成旋轉(zhuǎn)。...
    Richardo92閱讀 438評(píng)論 0 1
  • My code: My test result: 這道題目類(lèi)似于快排鏈表的一個(gè)小操作,換結(jié)點(diǎn)。但是這比插入排序鏈表...
    Richardo92閱讀 333評(píng)論 0 1
  • 1.reverently恭敬地 2.aglow泛紅光She was aglow with pleasure.她高興...
    Maei閱讀 599評(píng)論 0 0
  • 茶寵,顧名思義就是茶水滋養(yǎng)的寵物, 茶寵在茶湯的滋養(yǎng)下,年長(zhǎng)日久會(huì)變得溫潤(rùn)可人,茶香四溢,深得品茗人的寵愛(ài)。 對(duì)于...
    紫砂壺友閱讀 590評(píng)論 0 0

友情鏈接更多精彩內(nèi)容