package LinkedList;
/**
* @author:MindMrWang
*2017年12月3日
*:function:反转单向链表
*/
public class reverseList {
public static Node getReverseList(Node head) {
Node pre = null;//用来存储下反转后的第一个节点
Node next = null;//用来存储指向下一个节点
while(head != null) {
next = head.next;//每次都指向head的下一个节点
head.next = pre;//使得下一个节点等于第一个节点
pre = head;//使得反转后的头结点等于当前节点(这个链表在遍历,head一直指向当前遍历位置)
head = next;//head指向下一个节点
}
return pre;
}
}
public class Node {
public int value;
public Node next;
public Node() {
}
public Node(int data) {
this.value = data;
}
}
参考书籍:《程序员代码面试指南》