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

這次題目也不難波闹。先遍歷一遍找出總個數(shù) total
然后根據(jù) total - k 找到那個斷裂點一喘。斷開驱还。在用dummy和新的頭接上。
原來的頭接到tail后面去凸克,就差不多了议蟆。
主要我是沒能理解, k >= total 是什么意義萎战。
查了之后發(fā)現(xiàn)咐容,做一個 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;
        }
    }
}

有一個 corner case沒有考慮到蚂维,當k= 0 時戳粒,這個時候是不需要旋轉(zhuǎn)的,如果強制旋轉(zhuǎn)虫啥, newHead = tail.next -> null. 會出錯蔚约。

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 的時候,不需要rotate涂籽,直接返回就行苹祟。
Anyway, Good luck, Richardo! -- 08/16/2016

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市评雌,隨后出現(xiàn)的幾起案子树枫,更是在濱河造成了極大的恐慌,老刑警劉巖景东,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砂轻,死亡現(xiàn)場離奇詭異,居然都是意外死亡斤吐,警方通過查閱死者的電腦和手機搔涝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門厨喂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人体谒,你說我怎么就攤上這事杯聚。” “怎么了抒痒?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵幌绍,是天一觀的道長。 經(jīng)常有香客問我故响,道長傀广,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任彩届,我火速辦了婚禮伪冰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘樟蠕。我一直安慰自己贮聂,他們只是感情好,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布寨辩。 她就那樣靜靜地躺著吓懈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪靡狞。 梳的紋絲不亂的頭發(fā)上耻警,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天,我揣著相機與錄音甸怕,去河邊找鬼甘穿。 笑死,一個胖子當著我的面吹牛梢杭,可吹牛的內(nèi)容都是我干的温兼。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼式曲,長吁一口氣:“原來是場噩夢啊……” “哼妨托!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吝羞,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎内颗,沒想到半個月后钧排,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡均澳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年恨溜,在試婚紗的時候發(fā)現(xiàn)自己被綠了符衔。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡糟袁,死狀恐怖判族,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情项戴,我是刑警寧澤形帮,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站周叮,受9級特大地震影響辩撑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜仿耽,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一合冀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧项贺,春花似錦君躺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至啥箭,卻和暖如春谍珊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背急侥。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工砌滞, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人坏怪。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓贝润,卻偏偏與公主長得像,于是被迫代替她去往敵國和親铝宵。 傳聞我的和親對象是個殘疾皇子打掘,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容