ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

面试题6:从尾到头打印链表

2021-09-10 23:58:26  阅读:143  来源: 互联网

标签:Node 面试题 temp head value next 链表 从尾 节点


题目:输入一个链表的头节点,从尾到头反过来打印出每个系欸但的值。链表节点定义如下
本实现方法有头节点,头节点没有数据域

class Node{
	int value;
	Node next;
	
	public Node() {
	}
	public Node(int value) {
		this.value = value;
	}
		
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
}

第一种方法:使用栈结构打印

// 反向遍历链表  从未到头打印
	public void PrintListReversingly_IterativeLy(){ 
		if(head==null || head.next==null){
			throw new IllegalArgumentException("头节点为空,或链表为空");
		}
		// 利用栈结构将链表节点传入先进后出实现反向打印
		Stack<Node>	stack = new Stack<Node>();
		
		// 遍历链表将 遍历到的节点装入栈中
		//设置游标节点 第一个有效节点设为开始游标节点
		Node temp=head.next;
		
		while(temp!=null){
			// 将节点装如栈中
			stack.push(temp);
			// 接着往下遍历
			temp=temp.next;
		}
		
		// 一次将栈弹出
		while(!stack.empty()){
			System.out.println(stack.pop());
		}
	}

第二种:利用递归方法从尾到头

	// 递归打印节点
	public void PrintListReversingly_Recursively(Node head){
		if(head!=null){
			if(head.next.next!=null){
				PrintListReversingly_Recursively(head.next);
			}
			System.out.println(head.next);
		}
	}

完整源代码:

package com.basic;

import java.util.Scanner;
import java.util.Stack;

public class Queue_01 {
	// 创建头节点
	private static Node head=new Node(0);
	
	public static void main(String[] args) {
		Queue_01 queue = new Queue_01();
		Scanner sc=new Scanner(System.in);
		boolean isOK=true;
		while(isOK){
			System.out.println("输入  1 添加链表节点   2反序打印链表   3递归遍历  4退出");
			char c=sc.next().charAt(0);
			switch (c) {
			case '1':
				try{
					System.out.println("请输入想要添加的节点数字");
					String i=sc.next();
					Queue_01.Node node = new Queue_01.Node(Integer.parseInt(i));
					queue.AddByOrder(node);
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
				break;
			case '2':
				try{
					queue.PrintListReversingly_IterativeLy();
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
				break;
			case '3':
				queue.PrintListReversingly_Recursively(head);
				break;
			case '4':
				isOK=false;
				break;
			}
		}
	}
	
	public void AddByOrder(Node newNode){
		if(newNode==null){
			throw new IllegalArgumentException("传入节点有误,添加失败");
		}
		// 设立指针游标节点
		Node temp=head;
		// 设置添加标志位
		boolean isOK=false;
		
		// 开始遍历寻找插入位置 找到插入节点的上一个节点
		while(true){
			if(temp.next==null){
				// 说明当前节点应该插入到尾部
				break;
			}else if(temp.next.value==newNode.value){
				// 说明存在重复节点 不插入
				isOK=true;
				break;
			}else if(temp.next.value > newNode.value){
				// 找到了插入的位置
				break;
			}
			// 没有找到一直往下遍历 知道末尾节点
			temp=temp.next;
		}
		
		// 利用标志位 决定是否加入节点
		if(!isOK){
			// 将游标节点的下一个节点设置为新节点的下一个节点
			newNode.next=temp.next;
			// 将游标节点的下一个节点设置为新节点
			temp.next=newNode;
		}
	}
	
	// 反向遍历链表  从未到头打印
	public void PrintListReversingly_IterativeLy(){ 
		if(head==null || head.next==null){
			throw new IllegalArgumentException("头节点为空,或链表为空");
		}
		// 利用栈结构将链表节点传入先进后出实现反向打印
		Stack<Node>	stack = new Stack<Node>();
		
		// 遍历链表将 遍历到的节点装入栈中
		//设置游标节点 第一个有效节点设为开始游标节点
		Node temp=head.next;
		
		while(temp!=null){
			// 将节点装如栈中
			stack.push(temp);
			// 接着往下遍历
			temp=temp.next;
		}
		
		// 一次将栈弹出
		while(!stack.empty()){
			System.out.println(stack.pop());
		}
	}
	
	// 递归打印节点
	public void PrintListReversingly_Recursively(Node head){
		if(head!=null){
			if(head.next.next!=null){
				PrintListReversingly_Recursively(head.next);
			}
			System.out.println(head.next);
		}
	}
	
	static	class Node{
		int value;
		Node next;
		
		public Node() {
		}

		public Node(int value) {
			this.value = value;
		}

		@Override
		public String toString() {
			return "Node [value=" + value + "]";
		}
		
	}
}

此代码思路参考剑指offer 面试题

标签:Node,面试题,temp,head,value,next,链表,从尾,节点
来源: https://blog.csdn.net/qq_37203436/article/details/120231570

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有