算法与数据结构面试题(11)-一次遍历得到链表的中间节点

题目


如题


解题思路


参考


代码


public class Problem11 {
	LinkedListNode fast;// 每次前进两步的指针
	LinkedListNode slow;// 每次前进一步的指针

	public LinkedListNode getCenterNode(LinkedListNode node) {
		if (node == null)
			return node;
		fast = node;
		slow = node;

		while (fast != null && fast.getNextNode() != null) {
			fast = fast.getNextNode().getNextNode();
			slow = slow.getNextNode();
		}
		return slow;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}


你可能感兴趣的:(数据结构,算法)