定义一个单链表
public class ListNode {
public Integer value;
public ListNode next;
public ListNode(Integer value) {
this.value = value;
}
}
单链表反转
public static ListNode reverseList(ListNode head){
ListNode prev = null;
ListNode next = null;
while(head != null){
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
单链表取中间值
public static ListNode getMid(ListNode head){
ListNode fast = head;
ListNode slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}