========The end LinkedNode can not be deleted, why? =============

  • 0

    public class Solution {
    public void deleteNode(ListNode node) {
    if(node == null) return;

        ListNode n = node;
        ListNode nextN = n.next;
        while(nextN != null)
            n.val = nextN.val;
            //move to the next
            n = nextN;
            nextN = nextN.next;
        //nextN is null, n reached the end
        n = null;


  • 0

    I do not know it, either. Have you already got the answer?

  • 0

    ... pre_node -> tal

    Suppose that you are given a node which points to tal.

    tal is stored in your memory with a specific address ADR and that address is remembered by pre_node.next.

    You can delete node or you can change the value of tal.
    But no matter what you do, pre_node.next does not change and there is no way you can change it.

    When you print the linkedlist, pre_node.next is ADR which is not NULL, then you should visit the memory with address ADR and print whatever it is. So there is no way to delete the tal node.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.