反转单向和双向链表简化
来源:互联网 发布:网络电商招商 编辑:程序博客网 时间:2024/05/17 07:09
反转单向和双向链表简化:
//反转单向和双向链表public class ReverseList{ //节点的定义 public static class Node{ public int value; public Node next; public Node(int data) { this.value=data; } } //反转单向链表 public static Node ReverseOneList(Node head) { if(head==null|| head.next==null) { return head; } Node pre=null; Node last=null; while(head!=null) { last=head.next; head.next=pre; pre=head; head=last; } return pre; } //双向链表节点的定义 public static class TNode{ public int value; public TNode prev,next; public TNode(int data) { this.value=data; } } //反转双向链表 public static TNode ReverseTwoList(TNode head2) { while(head2==null||head2.next==null) { return head2; } TNode pre=null; TNode last=null; while(head2!=null) { last=head2.next; head2.next=pre; head2.prev=pre; pre=head2; head2=last; } return pre; } //打印链表 public static void PrintList(Node head) { while(head!=null) { System.out.print(head.value+" "); head=head.next; } System.out.println(); } //打印双链表 public static void PrintList2(TNode head) { while(head!=null) { System.out.print(head.value+" "); head=head.next; } System.out.println(); }public static void main(String []args){ Node node =new Node(1); node.next =new Node(2); node.next.next =new Node(3); //PrintList(node); Node mode=ReverseOneList(node); PrintList(mode); //双向链表的反转 TNode tnode=new TNode(1); tnode.next=new TNode(2); tnode.next.next=new TNode(3); tnode.next.prev=tnode; tnode.next.next.prev=tnode.next; //PrintList2(tnode); TNode t=ReverseTwoList(tnode); PrintList2(t); }}
阅读全文
0 0
- 反转单向和双向链表简化
- [链表]-反转单向和双向链表
- 反转单向链表和反转双向链表
- 反转单向链表和反转双向链表
- 数据结构 反转单向链表和双向链表
- 链表问题---反转单向和双向链表
- 链表问题——反转单向链表和双向链表
- 链表问题(3)-- 反转单向和双向链表
- 左程云_算法与数据结构 — 链表问题 — 04反转单向和双向链表
- 单向链表反转
- 反转单向链表
- 单向链表反转
- 单向链表反转
- 单向链表反转
- 反转单向链表
- 反转单向链表
- 反转单向链表
- 反转单向链表
- 64. Minimum Path Sum
- python用tqdm库来实现进度条功能
- 前端面试题
- java6
- html事件
- 反转单向和双向链表简化
- 编译那些事
- JVM笔记二
- NULL ,nullptr,nullptr_t,0 区别
- C语言基础之字符串(附测试代码)
- JS中 事件冒泡与事件捕获
- 聊天窗口
- 归并排序
- [题解]NOIP2015提高组の题解集合