LeetCode 链表的中间结点
Posted xhBruce
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 链表的中间结点相关的知识,希望对你有一定的参考价值。
876. 链表的中间结点
count 统计ListNode,折半循环查找链表节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode node = head;
int count = 0;
while (node != null) {
count++;
node = node.next;
}
count = count / 2;
while (count-- > 0) {
//System.out.println(count);
head = head.next;
}
return head;
}
}
以上是关于LeetCode 链表的中间结点的主要内容,如果未能解决你的问题,请参考以下文章