Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于链表删除倒数第k个节点的问题 #203

Open
Moziofmoon opened this issue Dec 19, 2018 · 1 comment
Open

关于链表删除倒数第k个节点的问题 #203

Moziofmoon opened this issue Dec 19, 2018 · 1 comment

Comments

@Moziofmoon
Copy link

`public static Node deleteLastKth(Node list, int k) {
Node fast = list;
int i = 1;
while (fast != null && i < k) {
fast = fast.next;
++i;
}

if (fast == null) return list; 

Node slow = list;
Node prev = null;
while (fast.next != null) {
  fast = fast.next;
  prev = slow;
  slow = slow.next;
}

if (prev == null) {
  list = list.next;
} else {
  prev.next = prev.next.next;
}
return list;

}`

感觉这段代码是删除正序的第k个节点的。不知道我的理解是否正确

@dickrsunny
Copy link

很显然是不正确的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants