反转单向和双向链表
2019-02-02 16:06
253 查看
[code]package practice; /** * 反转单向和双向链表 * 【题目】分别实现反转单向链表和反转双向链表的函数 * 【要求】如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度要求为O(1) * @author colin.chen * */ public class ReviseNode { public static class Node{ public int value; public Node next; public Node(int data){ this.value=data; } } public static Node reviseNodeList(Node head){ Node pre=null; Node next=null; while(head!=null){ next=head.next; head.next=pre; pre=head; head=next; } return pre; } public static class DoubleNode{ public int value; public DoubleNode next; public DoubleNode pre; public DoubleNode(int data){ this.value=data; } } public static void reviseDoubleNode(DoubleNode head){ DoubleNode pre=null; DoubleNode next=null; while(head!=null){ pre=head.pre; next=head.next; head.pre=next; head.next=pre; head=next; } } public static void main(String[] args) { Node head1 = new Node(1); head1.next = new Node(2); head1.next.next = new Node(3); // printLinkedList(head1); // head1 = reverseList(head1); // printLinkedList(head1); // // DoubleNode head2 = new DoubleNode(1); // head2.next = new DoubleNode(2); // head2.next.last = head2; // head2.next.next = new DoubleNode(3); // head2.next.next.last = head2.next; // head2.next.next.next = new DoubleNode(4); // head2.next.next.next.last = head2.next.next; // printDoubleLinkedList(head2); // printDoubleLinkedList(reverseList(head2)); } }
相关文章推荐
- 数据结构 反转单向链表和双向链表
- 链表问题——反转单向链表和双向链表
- 算法总结之 反转单向和双向链表
- 反转单向和双向链表简化
- 牛客网初级班------练习7之反转单向和双向链表
- 链表问题---反转单向和双向链表
- 反转单向链表和反转双向链表
- 反转链表 单向和双向链表 附代码
- [链表]-反转单向和双向链表
- [算法]反转单向链表和双向链表
- 左程云_算法与数据结构 — 链表问题 — 04反转单向和双向链表
- 单向链表的反转
- Java模拟单向链表和双向链表的实现
- Java实现单向链表反转
- 不产生新的链表,请写一段函数将一个单向链表反转顺序
- 数据结构学习之链表(单向、单循环以及双向)(递归实现)
- C 工具库 GLib --- 提供多种高级的数据结构,如内存块、双向和单向链表、哈希表、动态字符串等
- C语言基础—数据结构之单向循环链表和双向循环链表
- 反转单向链表的实现
- Java实现单向链表和双向链表