数据结构复习1——链表反转
来源:互联网 发布:网络运营商代理商 编辑:程序博客网 时间:2024/06/16 20:22
用了两种方式,递归和非递归的方式。
采
1)定义数据结构
class Node { //变量 private int record; //指向下一个对象 private Node nextNode; public Node(int record) { super(); this.record = record; } public int getRecord() { return record; } public void setRecord(int record) { this.record = record; } public Node getNextNode() { return nextNode; } public void setNextNode(Node nextNode) { this.nextNode = nextNode; } }
2.进行链表反转
public class ReverseSingleList { /** * 递归,在反转当前节点之前先反转后续节点 */ public static Node reverse(Node head) { if (null == head || null == head.getNextNode()) { //空结点或者没有后续结点 return head; } Node reversedHead = reverse(head.getNextNode());//反转后序结点 head.getNextNode().setNextNode(head); head.setNextNode(null); return reversedHead; } /** * 遍历,将当前节点的下一个节点缓存后更改当前节点指针 * */ public static Node reverse2(Node head) { if (null == head) { return head; } Node pre = head; Node cur = head.getNextNode(); Node next; while (null != cur) { next = cur.getNextNode(); cur.setNextNode(pre); pre = cur; cur = next; } //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head head.setNextNode(null); head = pre; return head; } public static void main(String[] args) { Node head = new Node(0); Node tmp = null; Node cur = null; // 构造一个长度为10的链表,保存头节点对象head for (int i = 1; i < 10; i++) { tmp = new Node(i); if (1 == i) { head.setNextNode(tmp); } else { cur.setNextNode(tmp); } cur = tmp; } //打印反转前的链表 Node h = head; while (null != h) { System.out.print(h.getRecord() + " "); h = h.getNextNode(); } //调用反转方法 head = reverse2(head); System.out.println("\n**************************"); //打印反转后的结果 while (null != head) { System.out.print(head.getRecord() + " "); head = head.getNextNode(); } } }自己动手实现链表的反转
package com.sxt.service;public class leiReverseList {public static void IteratorLst(Node head)//对链表进行迭代{Node currNode=null;currNode=head;while(currNode!=null){System.out.print( currNode.getRecord()+" ");currNode=currNode.getNextNode();}}public static Node reverseNode(Node n){if(n==null||n.getNextNode()==null)//空结点或者单结点直接返回即可{ return n;}Node subList=reverseNode(n.getNextNode());//将后面的子链表进行反转n.getNextNode().setNextNode(n);n.setNextNode(null);//n在反转之后总是在末尾 所以将其后续的结点赋值为空return subList;}public static Node reverseNode2(Node n)//非递归方式记性遍历{if(n==null)//如果是空结点 则直接进行返回即可{return n;}Node pre=n;//用来保存反转之后的内容Node curr=n.getNextNode();//要反转的当前位置while(curr!=null){Node next=curr.getNextNode();//用来保存尚未反转的内容curr.setNextNode(pre);//pre=curr;//pre结点前移,用来保存已经反转的数据curr=next;//}n.setNextNode(null);//此时原来的头结点已经放到最后的位置return pre;//真正的头结点改为当前节点}public static void main(String[] args) {Node head = new Node(0);//设置头结点Node currNode=null;//用来记录当前结点的位置for (int i = 1; i < 20; i++) {Node temp = new Node(i);if (i == 1) {head.setNextNode(temp); currNode=temp;//}else{ currNode.setNextNode(temp); currNode=temp;//指向下一个结点}}IteratorLst(head);System.out.println(); head=reverseNode2(head); IteratorLst(head); }}
0 0
- 数据结构复习1——链表反转
- Java链表反转(复习.复习)
- 数据结构—链表的复习
- 反转链表[数据结构]
- 反转链表[数据结构]
- 链表反转-数据结构
- 经典的数据结构——数组反转、链表反转、冒泡排序
- 【数据结构链表复习】
- 数据结构复习—栈
- 数据结构复习—队列
- 算法导论学习(1)——先复习复习数据结构
- 数据结构 -链表反转问题
- 数据结构之链表反转
- 数据结构之反转链表
- 数据结构 链表的反转
- 数据结构-反转链表详解
- 数据结构之链表反转
- 数据结构——反转单链表
- Android性能专项测试之Heap Snapshot工具
- 马良建模学习笔记——软包制作十四
- Linux下autoTools工具集使用介绍
- Linux下C++开发工具CodeBlocks安装技术方案
- 一次简单的微信秒杀活动
- 数据结构复习1——链表反转
- Android性能专项测试之Allocation Tracker(Android Studio)
- C++中的顶层const和底层const
- jquery中attr和prop的区别分析
- Xcode 【本地git 篇(1)】
- Android性能专项测试之Allocation Tracker(Device Monitor)
- linux下kill命令的几个参数分析
- <iOS>CGRect常见方法和几个CGRect函数
- 017redis3.0集群部署