在單鏈表和雙鏈表中刪除倒數第K個節點
分別實現兩個函數,一個可以刪除單鏈表中的倒數第K個節點,一個可以刪除雙鏈表中的倒數第k 個節點,要求時間復雜度是 O(N),空間復雜度是 O(1)。
【解析】
基本思路:
讓鏈表從頭開始走到尾,每移動一步,就讓k值減一,當k 值走到結尾時,
如果k 值大於0,說明鏈表根本沒有倒數第k 個節點
如果等於0,那么頭節點就是倒數第k 個節點,此時應該返回 head.next
如果小於0,則重新從頭節點開始,每移動一步,k 值增加一,當k 等於0時,移動停止,移動到的節點就是要刪除節點的上一個節點
package com.test; import com.test.ListNode; import com.test.ListNodeDouble; /** * Created by Demrystv. */ public class DelLastKNode { //刪除單鏈表中的倒數第K 個節點 public ListNode removeLastKthNodeInSingle(ListNode head, int lastKth){ if (head == null || lastKth < 1){ return null; } ListNode cur = head; while (cur != null){ lastKth--; cur = cur.next; } if (lastKth == 0){ head = head.next; } // 可以舉例子:1 2 3 4 5 的倒數第4個節點 if (lastKth < 0){ cur = head; while (++lastKth != 0){ cur = cur.next; } cur.next = cur.next.next; } return head; } // 刪除雙鏈表的倒數第K 個節點,原理與上面刪除倒數第K個 節點的原理類似,主要是注意指針的連接 public ListNodeDouble removeLastKthNodeInDouble(ListNodeDouble head, int lastKth){ if (head == null || lastKth < 1){ return null; } ListNodeDouble cur = head; while (cur != null){ lastKth--; cur = cur.next; } if (lastKth == 0){ head = head.next; head.last = null; } if (lastKth < 0){ cur = head; while (++lastKth != 0){ cur = cur.next; } ListNodeDouble newNext = cur.next.next; cur.next = newNext; if (newNext != null){ newNext.last = cur; } } return head; } }