1. package dsa.linkedlist;
  2. public class Node<E>{
  3. E data;
  4. Node<E> next;
  5. }
  1. package dsa.linkedlist;
  2. public class ReverseLinkedListRecursively {
  3. public static void main(String args[]) {
  4. ReverseLinkedListRecursively reverser = new ReverseLinkedListRecursively();
  5. SinglyLinkedList<Integer> originalList = reverser.getLabRatList(10);
  6. System.out.println("Original List : " + originalList.toString());
  7. originalList.start = reverser.reverse(originalList.start);
  8. System.out.println("Reversed List : " + originalList.toString());
  9. }
  10. public Node<Integer> reverse(Node<Integer> list) {
  11. if (list == null || list.next == null)
  12. return list;
  13. Node<Integer> nextItem = list.next;
  14. list.next = null;
  15. Node<Integer> reverseRest = reverse(nextItem);
  16. nextItem.next = list;
  17. return reverseRest;
  18. }
  19. private SinglyLinkedList<Integer> getLabRatList(int count) {
  20. SinglyLinkedList<Integer> sampleList = new SinglyLinkedList<Integer>();
  21. for (int i = 0; i < count; i++) {
  22. sampleList.add(i);
  23. }
  24. return sampleList;
  25. }
  26. }
  27. /*
  28. * SAMPLE OUTPUT Original List : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Reversed List : 9,
  29. * 8, 7, 6, 5, 4, 3, 2, 1, 0
  30. */
  1. package dsa.linkedlist;
  2. /**
  3. * This is a singly linked list with no prev pointer.
  4. * @author Braga
  5. * @param <E>
  6. */
  7. public class SinglyLinkedList<E> {
  8. Node<E> start;
  9. int size;
  10. public SinglyLinkedList(){
  11. start = null;
  12. size = 0;
  13. }
  14. //insertAtLast
  15. public void add(E data){
  16. insertAtLast(data);
  17. }
  18. public void insertAtLast(E data){
  19. if(size==0){
  20. start = new Node<E>();
  21. start.next = null;
  22. start.data = data;
  23. }else{
  24. Node<E> currentNode = getNodeAt(size-1);
  25. Node<E> newNode = new Node<E>();
  26. newNode.data = data;
  27. newNode.next = null;
  28. currentNode.next = newNode;
  29. }
  30. size++;
  31. }
  32. public void insertAtFirst(E data){
  33. if(size==0){
  34. start = new Node<E>();
  35. start.next = null;
  36. start.data = data;
  37. }else{
  38. Node<E> newNode = new Node<E>();
  39. newNode.data = data;
  40. newNode.next = start;
  41. start = newNode;
  42. }
  43. size++;
  44. }
  45. public Node<E> getNodeAt(int nodePos) throws ArrayIndexOutOfBoundsException{
  46. if(nodePos>=size || nodePos<0){
  47. throw new ArrayIndexOutOfBoundsException();
  48. }
  49. Node<E> temp = start;//Move pointer to front
  50. int counter = 0;
  51. for(;counter<nodePos;counter++){
  52. temp = temp.next;
  53. }
  54. return temp;
  55. }
  56. public void insertAt(int position, E data){
  57. if(position == 0){
  58. insertAtFirst(data);
  59. }else if(position==size-1){
  60. insertAtLast(data);
  61. }else{
  62. Node<E> tempNode = getNodeAt(position-1);
  63. Node<E> newNode = new Node<E>();
  64. newNode.data = data;
  65. newNode.next = tempNode.next;
  66. tempNode.next = newNode;
  67. size++;
  68. }
  69. }
  70. public Node<E> getFirst(){
  71. return getNodeAt(0);
  72. }
  73. public Node<E> getLast(){
  74. return getNodeAt(size-1);
  75. }
  76. public E removeAtFirst(){
  77. if(size==0){
  78. throw new ArrayIndexOutOfBoundsException();
  79. }
  80. E data = start.data;
  81. start = start.next;
  82. size--;
  83. return data;
  84. }
  85. public E removeAtLast(){
  86. if(size==0){
  87. throw new ArrayIndexOutOfBoundsException();
  88. }
  89. Node<E> tempNode = getNodeAt(size-2);
  90. E data = tempNode.next.data;
  91. tempNode.next = null;
  92. size--;
  93. return data;
  94. }
  95. public E removeAt(int position){
  96. if(position==0){
  97. return removeAtFirst();
  98. }else if(position == size-1){
  99. return removeAtLast();
  100. }else{
  101. Node<E> tempNode = getNodeAt(position-1);
  102. E data = tempNode.next.data;
  103. tempNode.next = tempNode.next.next;
  104. size--;
  105. return data;
  106. }
  107. }
  108. public int size(){
  109. return size;
  110. }
  111. public String toString(){
  112. if(size==0){
  113. return "";
  114. }else{
  115. StringBuilder output = new StringBuilder();
  116. Node<E> tempNode = start;
  117. while(tempNode.next!=null){
  118. output.append(tempNode.data).append(", ");
  119. tempNode = tempNode.next;
  120. }
  121. output.append(tempNode.data);
  122. return output.toString();
  123. }
  124. }
  125. }
 

单链表反转(Singly Linked Lists in Java)的更多相关文章

  1. 数据结构——单链表(singly linked list)

    /* singlyLinkedList.c */ /* 单链表 */ /* 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素. */ #include <stdio ...

  2. Java单链表反转 详细过程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...

  3. Java实现单链表反转操作

    单链表是一种常见的数据结构,由一个个节点通过指针方式连接而成,每个节点由两部分组成:一是数据域,用于存储节点数据.二是指针域,用于存储下一个节点的地址.在Java中定义如下: public class ...

  4. java 单链表反转

    最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...

  5. Java单链表反转图文详解

    Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...

  6. java实现单链表反转(倒置)

    据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; ...

  7. 单链表反转java代码

    据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. public class Node { int index; Nod ...

  8. java单链表反转

    今天做leetcode,遇到了单链表反转.研究了半天还搞的不是太懂,先做个笔记吧 参考:http://blog.csdn.net/guyuealian/article/details/51119499 ...

  9. C++单链表反转

    单链表反转笔记: #include<iostream> #include<string.h> using namespace std; struct ListNode { in ...

随机推荐

  1. 查看LOV对应查询语句的研究

    一.获取当前会话id 1.方法一 tools: Help > About 2.方法二 打开个性化定义界面(如果没有权限,到系统配置文件设置中,查看是否是"隐藏诊断菜单"被设置 ...

  2. 修改GDAL库支持RPC像方改正模型

    最近在做基于RPC的像方改正模型,方便对数据进行测试,修改了GDAL库中的RPC纠正模型,使之可以支持RPC像方改正参数. 下面是RPC模型的公式,rn,cn为归一化之后的图像行列号坐标,PLH为归一 ...

  3. Cocos2D:塔防游戏制作之旅(十三)

    让我们看一下Waves.plist文件,你将注意到它包含了3个数组.每一个数组表示一波攻击,也就是一组敌人一起到达闹事.第一个数组包含6个字典.每一个字典定义1个敌人. 在本次教程中,字典只存储敌人应 ...

  4. Java数组与函数的结合

    import java.util.Scanner; public class HelloWorld { public static void main(String[] args){ // Scann ...

  5. 从JDK源码角度看java并发线程的中断

    线程的定义给我们提供了并发执行多个任务的方式,大多数情况下我们会让每个任务都自行执行结束,这样能保证事务的一致性,但是有时我们希望在任务执行中取消任务,使线程停止.在java中要让线程安全.快速.可靠 ...

  6. Mybatis源码之(TypeAliasRegistry)TypeAlias别名实现机制

    在Mybatis编程中我们经常会用到将某个bean作为参数类型parameterType或者结果返回值类型ResultType,所以很多时候我们需要把完成的Bean的包名在mapper文件中写上,如下 ...

  7. Chapter 3 Protecting the Data(1):理解权限

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39548665,专题目录:http://blog.csdn.net/dba_huangzj ...

  8. C++ Primer 有感(面向对象编程)

    1.除了构造函数之外,任意非static成员函数都可以是虚函数.保留字virtual只在类内部的成员函数声明处出现,不能用在类定义体外部出现的函数定义上. 2.派生类只能通过派生类对象访问其基类的pr ...

  9. sql中with的用法(CTE公用表表达式):应用子查询嵌套,提高sql性能

    一.WITH AS的含义 WITH AS短语,也叫子查询部分(subquery factoring),定义一个SQL片断,该片断会被整个SQL语句所用到. 有时是为了让SQL语句的可读性更高些,也可能 ...

  10. VC++读取图像RGB值

    代码: #include <iostream> #include <fstream> #include <string> #include <windows. ...