Java 从链表末尾删除第k个节点

Java 从链表末尾删除第k个节点,java,linked-list,Java,Linked List,除了我想删除列表中的第一个节点(如果(kthToLast==0)head=head.next)。我不知道为什么它不删除第一个元素,因为head=head.next应该可以工作 谢谢 // 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 co

除了我想删除列表中的第一个节点(如果(kthToLast==0)head=head.next)。我不知道为什么它不删除第一个元素,因为
head=head.next应该可以工作

谢谢

// 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);
    }
}
//2.2实现一种算法,以查找单链表中最后一个元素的第k个元素。
公共类RemoveKthToLast{
静态节点头;
静态整数计数=0;
公共静态类节点{
int数据;
节点下一步;
节点(int数据){
这个数据=数据;
next=null;
计数++;
}
}
公共节点removeKthToLast(节点头,int n){
if(head==null | | n<1)
System.out.println(“无效”);;
整数计数=0;
节点温度=头;
while(temp!=null){//count列表中的节点数
温度=下一个温度;
计数++;
}
如果(n>计数)
System.out.println(“n大于列表长度”);;
int kthToLast=计数-n;
//删除第一个节点
如果(kthToLast==0)
head=head.next;
对于(int i=0;i
如果唯一的问题是没有删除第一个节点,请尝试此操作

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

问题是,即使在尝试删除链表中的第一个节点时,仍然会继续执行该方法的其余部分,只有在尝试删除第一个节点以外的节点时才应执行该方法。例如,假设您正在尝试移除头部。是,head字段将更新为列表中的第二个节点。但是,该方法并没有到此结束,而是执行行
head.next=head.next.next
,这将错误地删除链接列表中的第三个节点。因此,解决方案是在头部字段重置后基本结束该方法

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;
    }

只是一个建议:您可以在这里使用递归函数,您的代码将非常简单:

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;
}

谢谢你的反馈。但是,返回head似乎不起作用。我可以问一下,当您尝试删除第一个节点时,结果是什么吗?例如,如果链表是1->2->3->4(长度为4),并且我想删除列表末尾的第四个节点(因此n=4),那么结果节点应该是2->3->4,因为节点1是列表末尾的第四个节点。希望有帮助!我理解,但您当前获得的不正确输出是什么?另外,我可以在for循环中看到一些潜在的错误,因为您正在更新前面的内容以删除第kth-to-last节点,这可能会删除(kth-1)to-last节点之前的所有节点。因此,当k=4时,输出仍然是1->2->3->4,而不是2->3->4。真奇怪。哦,我运行了代码,for循环看起来很好。谢谢你的提醒。不幸的是,回过头来似乎不起作用。
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;
}