2.2问题:
实现一个算法,找出单向链表中倒数第k个节点。
链表数据:
import java.util.*;
//节点类
class Node {
protected Node next; //指针域
protected int data;//数据域
public Node( int data) {
this.data = data;
}
//显示此节点
public void display() {
System. out.print( data + " ");
}
}
//单链表
class LinkList {
public Node first; // 定义一个头结点
private int pos = 0;// 节点的位置
public LinkList() {
this.first = null;//this代表当前类
}
// 插入一个头节点
public void addFirstNode( int data) {
Node node = new Node(data);
node. next = first;
first = node;
}
// 删除一个头结点,并返回头结点
public Node deleteFirstNode() {
Node tempNode = first;
first = tempNode. next;
return tempNode;
}
// 在任意位置插入节点 在index的后面插入
public void add(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while ( pos != index) {
previous = current;
current = current. next;
pos++;
}
node. next = current;
previous. next = node;
pos = 0;
}
// 删除任意位置的节点
public Node deleteByPos( int index) {
Node current = first;
Node previous = first;
while ( pos != index) {
pos++;
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
pos = 0;
previous. next = current. next;
}
return current;
}
// 根据节点的data删除节点(仅仅删除第一个)
public Node deleteByData( int data) {
Node current = first;
Node previous = first; //记住上一个节点
while (current. data != data) {
if (current. next == null) {
return null;
}
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
previous. next = current. next;
}
return current;
}
// 显示出所有的节点信息
public void displayAllNodes() {
Node current = first;
while (current != null) {
current.display();
current = current. next;
}
System. out.println();
}
// 根据位置查找节点信息
public Node findByPos( int index) {
Node current = first;
if ( pos != index) {
current = current. next;
pos++;
}
return current;
}
// 根据数据查找节点信息
public Node findByData( int data) {
Node current = first;
while (current. data != data) {
if (current. next == null)
return null;
current = current. next;
}
return current;
}
}
方法一:递归,递归访问整个链表,当抵达链表末端时,该方法会传回一个置为0的计数器。之后的每次调用都会将这个计数器加一。当计数器等于k的时候,表示我们访问的是链表倒数第k个元素。
class nthToLast{
public static void main(String args[]){
LinkList linkList = new LinkList();
linkList.addFirstNode(19);//19
linkList.addFirstNode(18);//18,19
linkList.addFirstNode(15);//15,18,19
linkList.add(1, 16); //15,16,18,19
linkList.add(2, 17); //15,16,17,18,19
linkList.add(3, 17); //15,16,17,17,18,19
linkList.displayAllNodes();
Node node;
IntWrapper wrapper=new IntWrapper();
node = nthToLast(linkList.first,1,wrapper);
System.out.println(node.data);
}
public static class IntWrapper{
public int value = 0;
}
public static Node nthToLast(Node head,int k,IntWrapper i){
if(head == null){
return null;
}
Node node = nthToLast(head.next,k,i);
i.value=i.value+1;
if(i.value==k){
return head;
}
return node;
}
}
方法二:迭代法,使用两个指针p1 p2,并将它们指向链表中相距k个结点,具体做法是将p1和p2指向链表首结点,然后将p2向前移动k个结点。之后,我们以相同的速度移动这两个指针,p2会在移动length-k步后到达链表结尾,此时,p1会指向链表第length-k个结点,或者说倒数第k个结点。
class nthToLast{
public static void main(String args[]){
LinkList linkList = new LinkList();
linkList.addFirstNode(19);//19
linkList.addFirstNode(18);//18,19
linkList.addFirstNode(15);//15,18,19
linkList.add(1, 16); //15,16,18,19
linkList.add(2, 17); //15,16,17,18,19
linkList.add(3, 17); //15,16,17,17,18,19
linkList.displayAllNodes();
Node node;
node = nthToLast(linkList.first,1);
System.out.println(node.data);
}
public static Node nthToLast(Node head,int k){
if(k<=0) return null;
Node p1=head;
Node p2=head;
for(int i=0;i<k-1;i++){
if(p2==null) return null;//错误检查
p2=p2.next;
}
if(p2==null) return null;
while(p2.next!=null){
p1=p1.next;
p2=p2.next;
}
return p1;
}
}