算法总结之 反转单向和双向链表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法总结之 反转单向和双向链表相关的知识,希望对你有一定的参考价值。
分别实现反转单向和双向链表的函数
看代码:
package TT; public class Test88 { public class Node{ public int value; public Node next; public Node(int data){ this.value = data; } } public Node reverseList(Node head){ Node pre = null; Node next = null; while(head!=null){ next = head.next; head.next = pre; pre = head; head = next; } return pre; } }
反向双向链表
package TT; import TT.Test86.DoubleNode; public class Test89 { public DoubleNode{ public int value; public DoubleNode last; public DoubleNode pre; public DoubleNode(int data){ this.value = data; } } public DoubleNode reverseList(DoubleNode head){ DoubleNode pre = null; DoubleNode next = null; while(head!=null){ next = head.next; head.next = pre; head.last = next; pre = head; head = next; } return pre; } }
以上是关于算法总结之 反转单向和双向链表的主要内容,如果未能解决你的问题,请参考以下文章