19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.
For example,

   Given linked list: 1->2->3->4->5, and n = 2.
   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

Solution:Two pointers

One pass( no length precalc)
Time Complexity: O(N) Space Complexity: O(1)

Solution_a Code:
// with dummy startpoint

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // dummy -> 1  ->  2  ->  3  ->  4  ->  5  ->  null, n = 2
        //                      second    ---n+1---    first
        
        ListNode dummy = new ListNode(0);   // to deal with when n == list.length
        dummy.next = head;
        
        // init two pointers with n gap
        ListNode first = dummy, second = dummy;
        for(int i = 0; i < n + 1 && first != null; i++) {
            first = first.next;
        }
        
        // if(i < n + 1 && first == null) return null; // if n is not valid, but input is strict)
        
        // move the two ptrs until first one hits end
        while(first != null) {
            first = first.next;
            second = second.next;
        }
        
        // remove
        ListNode next = second.next;
        second.next = second.next.next;
        next.next = null;
        
        return dummy.next;
    }
}

Solution_b Code:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        
        // init two pointers with n gap
        ListNode first = head, second = head;
        for(int i = 0; i < n && first != null; i++) {
            first = first.next;
        }
        if(first == null) return head.next; // n == list.length, remove the first one  (or n is not valid, but input is strict)
        
        // move the two ptrs until first one hits end
        while(first.next != null) {
            first = first.next;
            second = second.next;
        }
        
        // remove
        ListNode next = second.next;
        second.next = second.next.next;
        next.next = null;
        
        return head;
    }
}

round1

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode slow = dummy;
        ListNode fast = dummy;
        
        for(int i = 0; i < n && fast.next != null; i++) {
            fast = fast.next;    
        }
        
        while(fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        
        // start deleting
        slow.next = slow.next.next;
        
        return dummy.next;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市涧至,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖袒炉,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件欺嗤,死亡現(xiàn)場離奇詭異灰羽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)惭蟋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來药磺,“玉大人告组,你說我怎么就攤上這事∮胛校” “怎么了惹谐?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長驼卖。 經(jīng)常有香客問我氨肌,道長,這世上最難降的妖魔是什么酌畜? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任怎囚,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘恳守。我一直安慰自己考婴,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布催烘。 她就那樣靜靜地躺著沥阱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伊群。 梳的紋絲不亂的頭發(fā)上考杉,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機(jī)與錄音舰始,去河邊找鬼崇棠。 笑死,一個胖子當(dāng)著我的面吹牛丸卷,可吹牛的內(nèi)容都是我干的枕稀。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼谜嫉,長吁一口氣:“原來是場噩夢啊……” “哼萎坷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起骄恶,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤食铐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后僧鲁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體虐呻,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年寞秃,在試婚紗的時候發(fā)現(xiàn)自己被綠了斟叼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡春寿,死狀恐怖朗涩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情绑改,我是刑警寧澤谢床,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站厘线,受9級特大地震影響识腿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜造壮,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一渡讼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦成箫、人聲如沸展箱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽混驰。三九已至,卻和暖如春凳厢,著一層夾襖步出監(jiān)牢的瞬間账胧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工先紫, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人筹煮。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓遮精,卻偏偏與公主長得像,于是被迫代替她去往敵國和親败潦。 傳聞我的和親對象是個殘疾皇子本冲,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

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