单链表反转(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 ...
随机推荐
- Spark:大数据的电花火石!
什么是Spark?可能你很多年前就使用过Spark,反正当年我四六级单词都是用的星火系列,没错,星火系列的洋名就是Spark. 当然这里说的Spark指的是Apache Spark,Apache Sp ...
- Activity与Fragment的生命周期详解
在安卓中Activity与Fragment是非常相似的两个类,它们各自都拥有自己的生命周期,且都可以用来显示布局文件中的视图.其中Activity是通过setContenView()显示视图,而Fra ...
- Dom4j修改xml文档引入
前面介绍了如何解析xnl文档的内容,这里对修改xml展开讨论. 一.首先看一下,写出内容到xml文档的主要代码: XMLWriter writer = new XMLWriter(OutputStre ...
- Demand Side Platform
DSP特点: DSP不是从网络媒体那里包买广告位,也不是采用CPD(Cost Per Day)的方式获得广告位:而是从广告交易平台(AdExchange)来通过实时竞价的方式获得对广告进行曝光的机会, ...
- Linux多线程实践(4) --线程特定数据
线程特定数据 int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); int pthread_key_ ...
- Linux多线程实践(2) --线程基本API
POSIX线程库 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以"pthread_"开头,要使用这些函数库,要通过引入头文<pthread.h>,而且链 ...
- numpy教程:矩阵matrix及其运算
http://blog.csdn.net/pipisorry/article/details/48791403 numpy矩阵简介 NumPy函数库中存在两种不同的数据类型(矩阵matrix和数组ar ...
- 9.7、Libgdx之振动器
(官网:www.libgdx.cn) 振动器允许你提醒手机用户. 振动器智能应用在Android设备中,需要特殊的权限: android.permission.VIBRATE 可以通过如下方式实现振动 ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- WebService详解(二)
WsExplorer和Tcp/Ip Monitor工具本身就存在于eclipse和MyEclipse中 使用工具的原因: 1. 使用工具可以更好的了解WebService请求的过程 2. 使 ...