简体   繁体   English

从链接列表的末尾删除第K个节点

[英]Removing Kth node from end of Linked List

Everything is working except for when I want to remove the first node in the list if (kthToLast == 0) head = head.next; 一切正常,除了当if (kthToLast == 0) head = head.next;想要删除列表中的第一个节点时if (kthToLast == 0) head = head.next; . I do not know why it does not remove the first element since head = head.next; 我不知道为什么它不删除第一个元素,因为head = head.next; should work. 应该管用。

Thanks. 谢谢。

// 2.2 Implement an algorithm to find the kth to last element of a singly linked list.

public class RemoveKthToLast {

    static Node head;
    static int count = 0;

    public static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            next = null;
            count++;
        }
    }

    public Node removeKthToLast(Node head, int n) {
        if (head == null || n < 1)
            System.out.println("invalid");;

        int count = 0;
        Node temp = head;
        while (temp != null) { // count number of nodes in list
            temp = temp.next;
            count++;
        }

        if (n > count)
            System.out.println("n is greater than length of list");;

        int kthToLast = count - n;

        // remove first node 
        if(kthToLast == 0) 
             head = head.next;

        for (int i = 0; i < kthToLast - 1; i++) 
            head = head.next;

        head.next = head.next.next;
        return head;
    }

    // prints out contents of linked-list
    public void toString(Node node) {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

    public static void main(String args[]) {

        RemoveKthToLast list = new RemoveKthToLast();

        list.head = new Node(1);
        list.head.next = new Node(2);
        list.head.next.next = new Node(3);
        list.head.next.next.next = new Node(4);

        list.removeKthToLast(head, 0);
        list.toString(head);
    }
}

If the only problem is that the first node isn't removed then try this 如果唯一的问题是未删除第一个节点,请尝试此操作

// remove first node 
if(kthToLast == 0) {
     head = head.next;
     return head;
}

The problem is that even when you're trying to delete the first node in the linked list, you still proceed with the rest of the method, which should only be executed when trying to remove nodes other than the first node. 问题在于,即使试图删除链表中的第一个节点,您仍然可以继续执行该方法的其余部分,该方法仅应在尝试删除除第一个节点以外的节点时执行。 For example, let's say you're trying to remove the head. 例如,假设您要移去头部。 Yes, the head field gets updated to the 2nd node in the list. 是的,头字段已更新为列表中的第二个节点。 However, the method doesn't end there and executes the line head.next = head.next.next which will incorrectly delete the 3rd node in the linked list. 但是,该方法并不止于此, head.next = head.next.next执行head.next = head.next.next ,这将错误地删除链接列表中的第三个节点。 Therefore, the solution is to basically end the method once the head field is reset. 因此,解决方案是一旦重置头字段,则基本上结束该方法。

public Node removeKthToLast(Node head, int n) {
    if (head == null || n < 1)
        System.out.println("invalid");;

    int count = 0;
    Node temp = head;
    while (temp != null) { // count number of nodes in list
        temp = temp.next;
        count++;
    }

    if (n > count)
        System.out.println("n is greater than length of list");;

    int kthToLast = count - n;

    // remove first node 
    if(kthToLast == 0){
         head = head.next;
         return head;//end the method here when removing the 1st node
    }
    for (int i = 0; i < kthToLast - 1; i++) 
        head = head.next;

    head.next = head.next.next;
    return head;
    }

Also, make sure to update your list's head node in the main. 另外,请确保更新主列表中的头节点。

list.head = list.removeKthToLast(head,4);

Just a suggestion: you could use a recursive function here and your code will be astoundly simple: 只是一个建议:您可以在此处使用递归函数,您的代码将非常简单:

public Node removeKthFromLast(Node head, int k) {
    int n;
    Node p = head;
    for (n=0;head != null;n++) {
      p = head.next;
    }
    return removeKthFromLastAux(head, n-k);
}

private Node removeKthFromLastAux(Node head, int ik) {
    if (head == null) {
        return null;
    }
    if (ik == 0) {
        return head.next;
    }
    head.next = removeKthFromLastAux(head, ik-1);
    return head;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM