Cracking the coding interview--Q2.2
2014-02-04 19:45
387 查看
题目
原文:
Implement an algorithm to find the nth to last element of a singly linked list.
译文:
实现一个算法找出一个单链表的倒数第n个元素。
解答
代码如下:
import java.util.*;
class Q2_2{
public static Object nthtoLastNode(LinkList ll,int n) {
Node head=ll.head;
Node node = head;
int count=n-1;
while (count >= 1 && node.next != null) {
count--;
node = node.next;
}
//链表的长度小于n
if (count != 0) return null;
while (node.next != null) {
head = head.next;
node = node.next;
}
return head.data;
}
public static void main(String[] args){
int[] data=new int[]{1,2,3,2,5,2};
LinkList ll=new LinkList();
for(int i=0;i<data.length;i++){
ll.add(data[i]);
}
System.out.println(nthtoLastNode(ll,4));
}
}
class Node{
public Object data;
public Node next;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkList{
public Node head;
public Node tail;
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void add(Object i){//在链表结尾插入节点
Node newnode = new Node();
newnode.setData(i);
if(head == null){
head = newnode;
tail = newnode;
size ++ ;
}
else {
tail.setNext(newnode);
tail = newnode;
size ++ ;
}
}
}
---EOF---
原文:
Implement an algorithm to find the nth to last element of a singly linked list.
译文:
实现一个算法找出一个单链表的倒数第n个元素。
解答
代码如下:
import java.util.*;
class Q2_2{
public static Object nthtoLastNode(LinkList ll,int n) {
Node head=ll.head;
Node node = head;
int count=n-1;
while (count >= 1 && node.next != null) {
count--;
node = node.next;
}
//链表的长度小于n
if (count != 0) return null;
while (node.next != null) {
head = head.next;
node = node.next;
}
return head.data;
}
public static void main(String[] args){
int[] data=new int[]{1,2,3,2,5,2};
LinkList ll=new LinkList();
for(int i=0;i<data.length;i++){
ll.add(data[i]);
}
System.out.println(nthtoLastNode(ll,4));
}
}
class Node{
public Object data;
public Node next;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkList{
public Node head;
public Node tail;
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void add(Object i){//在链表结尾插入节点
Node newnode = new Node();
newnode.setData(i);
if(head == null){
head = newnode;
tail = newnode;
size ++ ;
}
else {
tail.setNext(newnode);
tail = newnode;
size ++ ;
}
}
}
---EOF---
相关文章推荐
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q1.1
- Cracking the coding interview--Q3.5
- Cracking the coding interview--Q4.4
- Cracking the coding interview--Q3.1
- Cracking the coding interview--Q3.4
- 【Cracking the coding interview】Q1.6(旋转矩阵)
- Cracking the coding interview--Q4.8
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
- 《Cracking the Coding Interview》——第12章:测试——题目6
- Cracking the coding interview--Q20.9
- Cracking the coding interview--Q20.12
- Cracking the coding interview--Q5.6
- 《Cracking the Coding Interview》——第18章:难题——题目8
- Cracking the coding interview
- Cracking the coding interview--Q9.7