反转单向和双向链表简化
2017-10-29 16:21
591 查看
反转单向和双向链表简化:
//反转单向和双向链表
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);
}
}
//反转单向和双向链表
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);
}
}
相关文章推荐
- 数据结构 反转单向链表和双向链表
- [算法]反转单向链表和双向链表
- 左程云_算法与数据结构 — 链表问题 — 04反转单向和双向链表
- 反转链表 单向和双向链表 附代码
- [链表]-反转单向和双向链表
- 反转单向链表和反转双向链表
- 链表问题---反转单向和双向链表
- 链表问题——反转单向链表和双向链表
- 双向链表(3) - 反转双向链表
- C 工具库 GLib --- 提供多种高级的数据结构,如内存块、双向和单向链表、哈希表、动态字符串等
- 单向链表的反转
- Java模拟单向链表和双向链表的实现
- 反转部分单向链表
- 华为机试题(8)--单向链表的建立,反转(逆序),遍历显示,删除
- 以k个元素为一组反转单向链表
- 单向链表的反转
- [C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)
- 单向链表反转(循环和递归)
- 单向链表反转(倒置)问题
- 二维指针可以减少双向或单向链表的初始判断。