图解leetcode19. 删除链表的倒数第 N 个结点

1.题目描述:

给你一个链表,删除链表的倒数第n个结点,并且返回链表的头结点。

图解leetcode19. 删除链表的倒数第 N 个结点_第1张图片

 2.普通解法:

遍历两次链表,第一次得到链表长度,第二次找到倒数第n个结点并且删除,删除通过操作前驱结点完成,官解定义了一个方法和虚拟头结点,大同小异,代码如下:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode temp = head, temp1 = head;
        int count = 0, count1 = 0;
        while (temp != null) {
            temp = temp.next;
            count++;
        }
        if (n == count) return head.next;
        while (temp1 != null && count1 < count - n - 1) {
            temp1 = temp1.next;
            count1++;
        }
        temp1.next = temp1.next.next;
        return head;
    }
}

3.使用栈:

利用栈空间先进后出的特点,将倒数第n及之后元素弹出,省去了遍历得到结点总数,代码较简单直接看即可,官解里定义了虚拟头结点来避免删除第一个结点的情况。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        LinkedList stack = new LinkedList();
        ListNode loopTemp = head;
        while (loopTemp != null) {
            stack.push(loopTemp);
            loopTemp = loopTemp.next;
        }
        for (int i = 0; i < n; ++i) {
            stack.pop();
        }
        if (stack.isEmpty()) return head.next;
        stack.peek().next = stack.peek().next.next;
        return head;
    }
}

4.双指针:

使用双指针left和right来记录n的信息,避免遍历得到链表长度。left和right同时指向首位元素,先让right指针右移n,随后left和right指针同时右移直至right指针指向最后结点,此时left即为待删除结点的前驱结点。这里需要额外考虑删除首位结点的情况,这种情况下right指向末位元素的后一位为null,其他细节自己以示例走一遍即可。膜拜想出这个思路的大佬。。。代码和图解如下,思路理清了较易写出来。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode left = head;
        ListNode right = head;
        for (int i = 0;i < n;i++) {
            right = right.next;
        }
        if (right == null) return head.next;//删除首个结点的情况
        while (right.next != null) {//right遍历到最末位结点
            right = right.next;
            left = left.next;
        }
        left.next = left.next.next;
        return head;
    }
}

图解leetcode19. 删除链表的倒数第 N 个结点_第2张图片

5.二刷,双指针,加入虚拟头节点:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode virHead = new ListNode();
        virHead.next = head;
        ListNode temp = virHead;
        int right = 0;
        while (right < n) {
            temp = temp.next;
            right++;
        }
        ListNode temp1 = virHead;
        while (temp.next != null) {
            temp = temp.next;
            temp1 = temp1.next;
        }
        temp1.next = temp1.next.next;
        return virHead.next;
    }
}

6.二刷,栈,加入虚拟头节点:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode virNode = new ListNode();
        virNode.next = head;
        Stack stack = new Stack<>();
        ListNode temp = virNode;
        while (temp != null) {
            stack.push(temp);
            temp = temp.next;
        }
        int i = 0;
        while (i < n) {
            stack.pop();
            i++;
        }
        stack.peek().next = stack.peek().next.next;
        return virNode.next;
    }
}

你可能感兴趣的:(链表,链表,数据结构,算法,java,leetcode)