题目:输入一个链表的头节点,从尾到头反过来打印出每个节点的值。
思路:首先要询问是否允许改变链表结构,即改变指针方向。通常打印是读操作,不会改变链表结构。
一:使用栈基于循坏:打印每个节点,则一定会遍历链表,从头节点开始,将每一个节点的值存进栈结构中,在全部抛出来就可以实现逆序操作。
二:推之,递归本质就是栈结构,能用栈一定能用递归,用递归等待好处是代码简洁明了,坏处是当链表很长时,会导致函数调用的层级太深,最后函数调用栈溢出。
struct ListNode{
int m_nValue;
ListNode *m_pNext;
}
void PrintListReversingly_Iteratively(ListNode* pHead)
{
std::stack<ListNode*> nodes;
ListNode* pNode = pHead;
while(pNode != nullptr)
{
nodes.push(pNode);
pNode = pNode->m_pNext;
}
while(!nodes.empty())
{
pNode = nodes.top();
printf("%d\t",pNode->m_nValue);
nodes.pop;
}
}
void PrintListReversingly_Recursively(ListNode* pHead)
{
if(pHead != nullptr)
{
if(pHead->m_pNext != nullptr)
{
PrintListReversingly_Recursively(pHead->m_pNext);
}
printf("%d\t",pHeadd->m_nValue);
}
}
//Java链表的实现
package forl.girl.offer;
class Node{
Node next = null;
int data;
public Node(int data) {
this.data = data;
}
}
public class LinkList {
Node head = null;
/**
* 链表的插入
* @param d 插入链表的数据
*/
public void addNode(int d){
//新建一个节点
Node newNode = new Node(d);
//链表为空
if(null == head){
head = newNode;
return;
}
//循环找到链表的末尾
Node tmp = head;
while (tmp.next!=null){
tmp=tmp.next;
}
tmp.next = newNode;
}
/**
*
* @param index 链表的索引
* @return 如果索引小于1或者索引大于链表的长度返回false
* 删除成功返回true
*/
public boolean deleteNode(int index){
if(index<1||index>length()){
return false;
}
//特判删除头指针,因为单向链表没有前驱节点,删除比较简单
if(index == 1){
head = head.next;
return true;
}
int i=1;
Node preNode = head;
Node curNode = head.next;
while (curNode!=null){
//找到索引的位置,将i对应的节点删除,
//i的前驱节点指向index的后继节点(删除第index个节点)
if(i==index){
preNode.next = curNode.next;
return true;
}
preNode = curNode;
curNode = curNode.next;
i++;
}
return true;
}
/**
*
* @return 返回链表的长度
*/
public int length(){
int length = 0;
Node tmp = head;
while (tmp.next!=null){
length++;
tmp = tmp.next;
}
return length;
}
/**
*
* @return链表为空返回true
*/
public boolean isEmpty(){
return null == head?true:false;
}
/**
*
* @return返回排好序的链表
*/
public Node orderList(){
int tmp=0;
Node curNode = head;
Node nextNode = null;
while (curNode.next!=null){
nextNode = curNode.next;
//从小到大排序
while(nextNode!=null){
if(curNode.data > nextNode.data){
tmp = curNode.data;
curNode.data = nextNode.data;
nextNode.data = tmp;
}
nextNode = nextNode.next;
}
curNode = curNode.next;
}
return head;
}
/**
* 输出链表
*/
public void printList(){
Node tmp = head;
while(tmp!=null){
System.out.println(tmp.data);
tmp = tmp.next;
}
}
public static void main(String[] args) {
LinkList list = new LinkList();
list.addNode(10);
list.addNode(3);
list.addNode(5);
list.addNode(1);
list.addNode(9);
System.out.println("length is "+list.length());
System.out.println("before order");
list.printList();
list.orderList();
System.out.println("after order");
list.printList();
}
}
本题AC代码:
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(listNode==null) return list ;
Stack<ListNode> stack = new Stack<ListNode>();
while(listNode!=null){
stack.push(listNode);
listNode=listNode.next;
}
while (!stack.isEmpty()){
list.add(stack.pop().val);
}
return list;
}
}
总结:面试时如果需要修改面试数据一定要问面试官,是否允许修改。通常读操作时不会修改的。
链表逆向打印在于将每一个节点存进栈中,最后再从栈中抛出来。由栈又可以去联想到递归操作,而递归操作有一大坏处,就是当递归层级过深时,会导致栈溢出。