单链表反转(Singly Linked Lists in Java)
- package dsa.linkedlist;
- public class Node<E>{
- E data;
- Node<E> next;
- }
- package dsa.linkedlist;
- public class ReverseLinkedListRecursively {
- public static void main(String args[]) {
- ReverseLinkedListRecursively reverser = new ReverseLinkedListRecursively();
- SinglyLinkedList<Integer> originalList = reverser.getLabRatList(10);
- System.out.println("Original List : " + originalList.toString());
- originalList.start = reverser.reverse(originalList.start);
- System.out.println("Reversed List : " + originalList.toString());
- }
- public Node<Integer> reverse(Node<Integer> list) {
- if (list == null || list.next == null)
- return list;
- Node<Integer> nextItem = list.next;
- list.next = null;
- Node<Integer> reverseRest = reverse(nextItem);
- nextItem.next = list;
- return reverseRest;
- }
- private SinglyLinkedList<Integer> getLabRatList(int count) {
- SinglyLinkedList<Integer> sampleList = new SinglyLinkedList<Integer>();
- for (int i = 0; i < count; i++) {
- sampleList.add(i);
- }
- return sampleList;
- }
- }
- /*
- * SAMPLE OUTPUT Original List : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Reversed List : 9,
- * 8, 7, 6, 5, 4, 3, 2, 1, 0
- */
- package dsa.linkedlist;
- /**
- * This is a singly linked list with no prev pointer.
- * @author Braga
- * @param <E>
- */
- public class SinglyLinkedList<E> {
- Node<E> start;
- int size;
- public SinglyLinkedList(){
- start = null;
- size = 0;
- }
- //insertAtLast
- public void add(E data){
- insertAtLast(data);
- }
- public void insertAtLast(E data){
- if(size==0){
- start = new Node<E>();
- start.next = null;
- start.data = data;
- }else{
- Node<E> currentNode = getNodeAt(size-1);
- Node<E> newNode = new Node<E>();
- newNode.data = data;
- newNode.next = null;
- currentNode.next = newNode;
- }
- size++;
- }
- public void insertAtFirst(E data){
- if(size==0){
- start = new Node<E>();
- start.next = null;
- start.data = data;
- }else{
- Node<E> newNode = new Node<E>();
- newNode.data = data;
- newNode.next = start;
- start = newNode;
- }
- size++;
- }
- public Node<E> getNodeAt(int nodePos) throws ArrayIndexOutOfBoundsException{
- if(nodePos>=size || nodePos<0){
- throw new ArrayIndexOutOfBoundsException();
- }
- Node<E> temp = start;//Move pointer to front
- int counter = 0;
- for(;counter<nodePos;counter++){
- temp = temp.next;
- }
- return temp;
- }
- public void insertAt(int position, E data){
- if(position == 0){
- insertAtFirst(data);
- }else if(position==size-1){
- insertAtLast(data);
- }else{
- Node<E> tempNode = getNodeAt(position-1);
- Node<E> newNode = new Node<E>();
- newNode.data = data;
- newNode.next = tempNode.next;
- tempNode.next = newNode;
- size++;
- }
- }
- public Node<E> getFirst(){
- return getNodeAt(0);
- }
- public Node<E> getLast(){
- return getNodeAt(size-1);
- }
- public E removeAtFirst(){
- if(size==0){
- throw new ArrayIndexOutOfBoundsException();
- }
- E data = start.data;
- start = start.next;
- size--;
- return data;
- }
- public E removeAtLast(){
- if(size==0){
- throw new ArrayIndexOutOfBoundsException();
- }
- Node<E> tempNode = getNodeAt(size-2);
- E data = tempNode.next.data;
- tempNode.next = null;
- size--;
- return data;
- }
- public E removeAt(int position){
- if(position==0){
- return removeAtFirst();
- }else if(position == size-1){
- return removeAtLast();
- }else{
- Node<E> tempNode = getNodeAt(position-1);
- E data = tempNode.next.data;
- tempNode.next = tempNode.next.next;
- size--;
- return data;
- }
- }
- public int size(){
- return size;
- }
- public String toString(){
- if(size==0){
- return "";
- }else{
- StringBuilder output = new StringBuilder();
- Node<E> tempNode = start;
- while(tempNode.next!=null){
- output.append(tempNode.data).append(", ");
- tempNode = tempNode.next;
- }
- output.append(tempNode.data);
- return output.toString();
- }
- }
- }
单链表反转(Singly Linked Lists in Java)的更多相关文章
- 数据结构——单链表(singly linked list)
/* singlyLinkedList.c */ /* 单链表 */ /* 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素. */ #include <stdio ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- Java实现单链表反转操作
单链表是一种常见的数据结构,由一个个节点通过指针方式连接而成,每个节点由两部分组成:一是数据域,用于存储节点数据.二是指针域,用于存储下一个节点的地址.在Java中定义如下: public class ...
- java 单链表反转
最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...
- Java单链表反转图文详解
Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...
- java实现单链表反转(倒置)
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; ...
- 单链表反转java代码
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. public class Node { int index; Nod ...
- java单链表反转
今天做leetcode,遇到了单链表反转.研究了半天还搞的不是太懂,先做个笔记吧 参考:http://blog.csdn.net/guyuealian/article/details/51119499 ...
- C++单链表反转
单链表反转笔记: #include<iostream> #include<string.h> using namespace std; struct ListNode { in ...
随机推荐
- 下载android4.4.2源码全过程(附已下载的源码)
今天在下载andriod源码,特来与大家分享一下我的经验.当然,网上教下载源码的教程较多,本文主要针对在GFW下下载源码出现的各种问题的解决方法. 1.首先安装下载客户端git , curl. 命令如 ...
- linux命令指usermod(管理用户以及权限的命令)
usermod命令:用来修改用户帐号的各项设定. 示例:usermod -a -G usergroupnewuser 或者usermod -aGusergroup newuser 语法:usermod ...
- A*寻路算法入门(五)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 1022. Digital Library (30) -map -字符串处理
题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...
- Guava 教程2-深入探索 Google Guava 库
原文出处: oschina 在这个系列的第一部分里,我简单的介绍了非常优秀的Google collections和Guava类库,并简要的解释了作为Java程序员,如果使用Guava库来减少项目中大量 ...
- 通过一个tomcat端口访问多个tomcat项目 tomcat转发
需求是这样的,有一个tomcat,是80端口,现在我要通过这个tomcat转发到服务器其他tomcat,其他tomcat的端口不是80.这样做就可以避免这样www.baidu.com:8081的情况. ...
- 内存管理Memory OC——第九天
1. 内存管理方式 垃圾回收机制:(Garbage Collection),有系统管理内存,开发人员需要管理 注:OC从2.0之后就开始支持垃圾回收机制,但是只适用 ...
- 【Android 应用开发】Android 平台 HTTP网速测试 案例 API 分析
作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速测试标准 : 除普通网页测速 ...
- json进阶(一)js读取解析JSON类型数据
js读取解析JSON类型数据 一.什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式,同 ...
- android动画之interpolator和typeEvaluator用法详解
Interpolator (插值器) 我们在写动画的时候为了达到某种效果往往需要设置插值器,用来真实的模拟生活中的场景. Interpolator (插值器)被用来修饰动画效果,定义动画的变化率,可以 ...