单链表反转(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 ...
随机推荐
- [Mysql]备份同库中一张表的历史记录 insert into ..select
需求 现在有个这么一个需求,mysql中有个表,数据增长的很快,但是呢这个数据有效期也就是1个月,一个月以前的记录不太重要了,但是又不能删除.为了保证这个表的查询速度,需要一个简单的备份表,把数据倒进 ...
- UE4使用widget创建UI界面播放视频
我的目的非常简单,点击按钮,播放或暂停场景中的视频 1.准备了一个mp4视频资源,为视频资源创建了一个Media Texture,在Media Player中选择导入进来的视频资源,再为Media T ...
- 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
只有程序员懒起来,才能提高开发效率 233333 在MVP的使用过程中,我们需要反复的去写各种MVP的接口和实现类, 实在是 太麻烦了!!所以抽时间撸了一款插件(只可用于Intellj IDEA 和 ...
- 轻松学习Asp.net中的控件
C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(四)
写到这突然有童鞋质疑,你这哪里是牛,分明是熊嘛! 仔细看了下,还真像牛.反正是这个意思.怪本猫猪牛熊不分,好在道理是一样的. 下面继续,言归正传. 添加一个空白的touchBegan方法,如果没有这个 ...
- 从Linux启动过程到android启动过程
Linux启动过程: 1.首先开机给系统供电,此时硬件电路会产生一个确定的复位时序,保证cpu是最后一个被复位的器件.为什么cpu要最后被复位呢?因为 如果cpu第一个被复位,则当cpu复位后开始运行 ...
- python一行写不下,变多行
python里一行写不下,拆成多行, \和() 两种方法 在一行末尾 加上" \",也就是空格加上\ a= 'sdfaf' \ 'test' 注意两个对象都要独立,字符串 ...
- MacBook 最近发现的一些问题和技巧
本猫的mba最近键盘莫名会失灵,但用鼠标切换其他用户时时好的,切换回来又不行,体现如下: 1.Spotlight里可以输入,其他不可以 2.cmd+tab可以切换进程 现在只有重启后才可以恢复. 网上 ...
- RHEL6.4上升级python从2.6.6到2.7.3
RHEL6.4上升级python从2.6.6到2.7.3 原始安装好的redhat6.4上的python版本是2.6.6,不能满足实际需要.升级的方法很多,从源码升级或者从rpm包升级.其中从rpm包 ...
- android重启代码
首先新建一个app然后添加 android:sharedUserId="android.uid.system" 再添加重启的权限 <uses-permission andro ...