package com.my.util;
public class LinkNode {
public int value;
public LinkNode next;
public LinkNode(int value) {
this.value = value;
}
}
package com.my.util;
public class DoubleNode {
public int value;
public DoubleNode next;
public DoubleNode last;
public DoubleNode(int value) {
this.value = value;
}
}
package com.my.suanfa;
import com.my.util.DoubleNode;
import com.my.util.LinkNode;
public class Solution08 {
public LinkNode removeLastKthNode(LinkNode head, int lastKth) {
if(head == null || lastKth <1) {
return head;
}
LinkNode cur = head;
while(cur != null) {
lastKth--;
cur = cur.next;
}
if(lastKth == 0) {
head = head.next;
}
if(lastKth < 0) {
cur = head;
while(++lastKth != 0) {
cur = cur.next;
}
cur.next = cur.next.next;
}
return head;
}
public DoubleNode removeLastKthNode(DoubleNode head, int lastKth) {
if(head == null || lastKth < 1) {
return head;
}
DoubleNode cur = head;
while(cur != null) {
lastKth--;
cur = cur.next;
}
if(lastKth == 0) {
head = head.next;
head.last = null;
}
if(lastKth < 0) {
cur = head;
while(++lastKth != 0) {
cur = cur.next;
}
DoubleNode newNext = cur.next.next;
cur.next = newNext;
if(newNext != null) {
newNext.last = cur;
}
}
return head;
}
}