美文网首页
876. Middle of the Linked List (

876. Middle of the Linked List (

作者: f1a94e9a1ea7 | 来源:发表于2018-11-18 15:54 被阅读9次

Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
给一个有头结点的非空单链表,返回中间结点。
如果有两个中间结点,返回第二个。

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

解析:

参考于桐哥优秀

第一种方法:先遍历整个单链表,得到长度,再重新遍历到中间位置。
但是这样就需要循环两次,时间复杂度为 2O(n)

第二种方法:只遍历一遍,使用两个指针 tail 和 mid,tail 指向尾部,tail 每走两步,mid 走一步;结束之后,若 tail 最后走的是一步,说明 tail 走了 (2n +1) 步,总共是偶数个节点,此时 mid 还要走一步

  • 具体操作是:
    1. 两个指针 tail 和 mid ,同时从头结点出发
    2. 有一个计数器初始为 0 ,tail 走一步,计时器加 1
    3. 当计时器等于 2 ,也就是 tail 走了两步的时候,让 mid 走一步,同时初始化计数器
    4. 当 tail 走到最后一步了,也就是指针指向 null
    5. 如果计时器为 1 ,说明是偶数个结点(别忘了还有一个头结点),所以 mid 还要走一步
    6. 如果计时器为 2,那就直接返回 mid 当前指向的结点

(The following English translation may not be right)

analyze:

refer:桐哥优秀

  • The first method: traversing the linked list and get its length, then traversing to the middle of the linked list
    the first method needs to traverse twice, the time complexity is 2O(n)

  • The second method: just traverse it once, use two pointers: tail and mid, tail points to the tail, when tail takes two steps, the mid points takes one step. when the tail points arriving the last nodes, if the tail just takes one step, means there is an even number of nodes, so the mid has to take a step.

  • The operate:

    1. Both of the two points starting from the head node
    2. Declare a counter, the value is 0 , the value add 1 while the tails points takes a step
    3. when the value is equal to 2 , mid take one step, and initialize the counter
    4. when tail points to null
    5. if the counter value is 1 , it means there is an even number of nodes ( because the linked list has head node ) , so the mid else need take a step.
    6. if the counter value is 2 , just return the mid.
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
  var tail = mid = head;  
  var count = 0;
  while (tail.next !== null) {
    tail = tail.next;
    count ++;
    if (count === 2) {
      mid = mid.next;
      count = 0;
    }
  }
  if (count === 1) {
    mid = mid.next;
  }
  return mid;

};

相关文章

网友评论

      本文标题:876. Middle of the Linked List (

      本文链接:https://www.haomeiwen.com/subject/gtlgfqtx.html