您的位置:首页 > Web前端 > Node.js

Java实现ListNode链表反转-三种方法

2019-07-08 12:25 1551 查看

链表是一种常见的数据结构,由一个个节点通过指针连接而成。每个链表节点由数据域指针域组成,其中数据域用来存储数据data,指针域用来存储指向下一节点的指针next,即指向下一节点的地址。

[code]public class ListNode {

Object data;//数据域
ListNode next;//指针域

public ListNode(Object data){
this.data = data;
this.next = null;
}

}

链表反转的操作是指原本链表中的某一节点的指针是指向后一个节点的,现在需要将此节点的指针指向前一节点。首先需要定义三个指针,第一个是指向当前节点的指针cur,第二个是指向当前节点cur的前一节点指针pre,第三个是辅助指针temp。步骤如下:

方法一(头插法):

[code]public  static ListNode  reverseListNode1(ListNode head){
if(head == null || head.next == null)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
//定义前一节点指针,当前节点指针,辅助指针(后一节点指针)
ListNode pre = dummy, cur = head, tmp;
while(cur != null && cur.next !=null){
tmp = cur.next;   //用于保存当前节点指针cur的后一节点指针
cur.next = tmp.next;  //更新cur指针
tmp.next = pre.next;  //将当前节点指针cur后一节点的指针指向cur(即指针指向顺序颠倒)
pre.next = tmp;       //更新pre指针
}
return dummy.next; //返回新的头结点
}

(1)//定义前指针,当前指正,辅助指针
             ListNode pre = dummy, cur = head, tmp;

接下来遍历链表,一步步执行链表反转操作。

 

(2)tmp = cur.next;   //用于保存当前节点指针cur的后一节点指针

(3)cur.next = tmp.next;  //将当前节点指针指向tmp后一节点(更新cur指针指向)

(4)tmp.next = pre.next;  //将后一节点指针temp指向当前节点

(5)pre.next = tmp;   将pre节点指向tmp(更新pre指针指向)

然后继续遍历重复以上操作,即可反转列表。

方法二(普通方法):

[code] public  static ListNode  reverseListNode2(ListNode head){
if(head == null || head.next ==null)
return head;
ListNode pre = null;    //前一节点指针
ListNode cur = head;   //当前节点指针
ListNode tmp;          //后一节点指针
while(cur != null){
tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}

方法三(递归方法):

[code] public static ListNode reverseListNode2(ListNode node){
//通过层层改变节点的指向来进行反转链表
if(node.next == null){
return node;
}
ListNode p = reverseListNode2(node.next);
node.next.next = node;
node.next = null;
return p;
}

Java代码测试

[code]public class ReverseListNode {

//方法1(头插法)
public  static ListNode  reverseListNode1(ListNode head){
if(head == null || head.next == null)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
//定义前指针,当前指正,辅助指针
ListNode pre = dummy, cur = head, tmp;
while(cur != null && cur.next !=null){
tmp = cur.next;   //用于保存当前节点指针cur的后一节点指针
cur.next = tmp.next;  //更新cur指针
tmp.next = pre.next;  //将当前节点指针cur后一节点的指针指向cur(即指针指向顺序颠倒)
pre.next = tmp;       //更新pre指针
}
return dummy.next; //返回新的头结点
}

//方法2(递归法)
public static ListNode reverseListNode2(ListNode node){
//通过层层改变节点的指向来进行反转链表
if(node.next == null){
return node;
}
ListNode p = reverseListNode2(node.next);
node.next.next = node;
node.next = null;
return p;
}

//方法3(普通方法)
public  static ListNode  reverseListNode3(ListNode head){
if(head == null || head.next ==null)
return head;
ListNode pre = null;    //前一节点指针
ListNode cur = head;   //当前节点指针
ListNode tmp;          //后一节点指针
while(cur != null){
tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}

public  static  void main(String args[]){
ListNode head = new ListNode(0);
ListNode node1 = new ListNode(1);
head.next = node1;
ListNode node2 = new ListNode(2);
node1.next = node2;
ListNode node3 = new ListNode(3);
node2.next = node3;
ListNode node4 = new ListNode(4);
node3.next = node4;
head = reverseListNode1(head);
while(head != null){
System.out.println(head.data);
head = head.next;
}
}

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: