要求:
分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点。如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1)。
补充:
链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必按顺序存储,链表在插入的时候可以达到O⑴的复杂度,比另一种线性表:顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O⑴。使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。
思考:
如果链表为空或者K小于1,这种情况下参数是无效的,直接返回即可。除此之外,让链表从头开始走到尾,每移动一步,就让K的值减1。
例如链表为:1->2->3,k = 4,链表根本不存在倒数第4个节点。
有三种情况:
第一,当链表走到结尾时,如果K值大于0,说明不用调整,链表里根本没有倒数第k个节点。
第二,当链表走到结尾时,如果K值等于0,说明链表倒数第K个位头节点,此时直接返回head.next。
第三,当链表走到结尾时,如果K值小于0,则重新从头节点开始走,每走一步就让K的值加1,当K等于0时,移动停止,移动到的节点就是要删除节点的前一个节点。
实现代码:
Node.java:
package algorithm_9;
public class Node {
public int value;
public Node next;
public Node (int data){
this.value = data;
}
}
DoubleNode.java:
package algorithm_9;
public class DoubleNode {
public int value;
public DoubleNode next;
public DoubleNode (int data){
this.value = data;
}
}
algorithm_9.java:
package algorithm_9;
public class algorithm_9 {
public static Node removeLastKthNode(Node head, int lastKth){
if(head == null || lastKth <1){
return head;
}
Node 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 static DoubleNode removeLastKthNode2(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;
}
if(lastKth < 0){
cur = head;
while(++lastKth != 0){
cur = cur.next;
}
cur.next = cur.next.next;
}
return head;
}
public static void print_LinkedList(Node head){
Node cur = head;
while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.print(" \n");
}
public static void print_LinkedList(DoubleNode head){
DoubleNode cur = head;
while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.print(" \n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Node singleLinkedList = new Node(1);
singleLinkedList.next = new Node(2);
singleLinkedList.next.next = new Node(3);
System.out.print("before-Operation-singleLinkedList: \n");
print_LinkedList(singleLinkedList);
singleLinkedList = removeLastKthNode(singleLinkedList, 2);
System.out.print("After-Operation-singleLinkedList: \n");
print_LinkedList(singleLinkedList);
DoubleNode DoubleLinkedList = new DoubleNode(1);
DoubleLinkedList.next = new DoubleNode(2);
DoubleLinkedList.next.next = new DoubleNode(3);
System.out.print("before-Operation-DoubleLinkedList: \n");
print_LinkedList(DoubleLinkedList);
DoubleLinkedList = removeLastKthNode2(DoubleLinkedList, 2);
System.out.print("After-Operation-DoubleLinkedList: \n");
print_LinkedList(DoubleLinkedList);
}
}
实验结果:
before-Operation-singleLinkedList:
1 2 3
After-Operation-singleLinkedList:
1 3
before-Operation-DoubleLinkedList:
1 2 3
After-Operation-DoubleLinkedList:
1 3