数据结构(二)之链表反转
生活随笔
收集整理的這篇文章主要介紹了
数据结构(二)之链表反转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、鏈表反轉
1、反轉非遞歸算法
2、反轉遞歸算法
鏈表結點:
package cn.edu.scau.mk;/**** @author MK* @param <T>*/ public class Node<T> {private T data;private Node<T> next = null;public Node(T data) {this.data = data;}public T getData() {return data;}public void setData(T data) {this.data = data;}public Node<T> getNext() {return next;}public void setNext(Node<T> next) {this.next = next;}} View Code?鏈表:
package cn.edu.scau.mk;import java.util.Comparator;/**** @author MK* @param <T>*/ public class LinkedList<T> {protected Node<T> head = null;/*** 添加** @param data*/public void add(T data) {//頭結點為nullif (head == null) {head = new Node<>(data);return;}//尋找末結點Node<T> curNode = head;while (curNode.getNext() != null) {curNode = curNode.getNext();}curNode.setNext(new Node<>(data));//添加結點 }/*** 刪除** @param index 下標,從0開始* @return*/public boolean delete(int index) {//沒有數據if (head == null) {return false;}//刪除頭結點if (index == 0) {head = head.getNext();}Node<T> curNode = head;int i = 1;while (curNode.getNext() != null) {if (i == index) {curNode.setNext(curNode.getNext().getNext());return true;}i++;curNode = curNode.getNext();}throw new IndexOutOfBoundsException("Index: "+index+", Size: "+i);}/*** 長度** @return*/public int length() {int len = 0;Node<T> curNode = head;while (curNode != null) {len++;curNode = curNode.getNext();}return len;}/*** 查找* @param index 位置* @return */public T get(int index) {Node<T> curNode = head;int i = 0;while (curNode != null) {if (i == index) {return curNode.getData();}i++;curNode = curNode.getNext();}throw new IndexOutOfBoundsException("Index: "+index+", Size: "+i);}/*** 排序* @param comparator 比較器*/public void sort(Comparator<T> comparator) {//沒有數據if (head == null) {return;}Node<T> curNode = head;Node<T> nextNode;Node<T> minNode;while (curNode.getNext() != null) {minNode = curNode; //默認最小結點為當前結點nextNode = curNode.getNext(); //下一個結點while (nextNode != null) {//比當前結點小,記錄最小結點if(comparator.compare(curNode.getData(), nextNode.getData())>0){minNode=nextNode;}nextNode=nextNode.getNext(); //繼續與下一個結點比較 }//最小結點不是當前結點,交換數據if(minNode!=curNode){T data=curNode.getData();curNode.setData(minNode.getData());minNode.setData(data);}curNode=curNode.getNext(); //移至下一個結點 }}/*** 打印輸出*/public void print() {Node<T> curNode = head;while (curNode!=null) { System.out.print(curNode.getData()+" ");curNode=curNode.getNext();}System.out.println();} } View Code?
二、非遞歸算法
package cn.edu.scau.mk;/**** @author MK* @param <T>*/ public class ReverseLinkedList<T> extends LinkedList<T>{/*** 反轉*/public void reverse() {//沒有數據if (head == null) {return;}Node<T> curNode = head.getNext();Node<T> preNode = head;Node<T> nextNode;while (curNode != null) {nextNode = curNode.getNext();curNode.setNext(preNode);preNode = curNode;curNode = nextNode;}head.setNext(null);head = preNode;} }?
?
三、遞歸算法
package cn.edu.scau.mk;/**** @author MK* @param <T>*/ public class ReverseLinkedList<T> extends LinkedList<T>{/*** 遞歸反轉*/public void reverse1() {head = reverseByCall(head);}/*** 遞歸函數* @param headNode 頭結點* @return*/private Node reverseByCall(Node<T> headNode) {//沒有數據if (headNode == null || headNode.getNext() == null) {return headNode;}Node<T> curNode = headNode.getNext();Node<T> rootNode = reverseByCall(curNode);curNode.setNext(headNode);headNode.setNext(null);return rootNode;}}?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的数据结构(二)之链表反转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构(一)之链表
- 下一篇: 数据结构(三)之单链表反向查找