链表模板总结
时间:2017-03-09 00:00:02
收藏:0
阅读:223
单链表:
public class ListNode { int val; ListNode next; ListNode (int val) { this.val = val; } }
1、反转单链表

public void reverseLinkedList(ListNode head) { ListNode pre = null; while (head != null) { ListNode next = head.next; head.next = pre; pre = head; head = next; } return pre; }
2、寻找单链表的中点

public ListNode findMid(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } return slow; }
原文:http://www.cnblogs.com/zhiyangjava/p/6523481.html
评论(0)