leetcode 876. Middle of the Linked List intermediate node linked list pointer speed

leetcode 876. Middle of the Linked List intermediate node linked list pointer speed

leetcode 2020 March 1 question daily punch

Title:
Given a non-empty list with a single head of the first node, an intermediate node returns the list. If there are two intermediate nodes, the second intermediate node is returned.

Example 1:
Input: [1,2,3,4,5]
Output: this list node 3 (SEQ form: [3,4,5])
Returns the value of node 3. (This evaluation system is expression of the sequence node [3,4,5]).
Note that, we return an object of type ListNode ANS, so that:
. Ans.val. 3 =, = ans.next.val. 4, ans.next.next.val =. 5, and ans.next.next.next = NULL
Example 2:
input: [1,2,3,4,5,6]
output: this list node 4 (SEQ form: [4,5,6])
Since the list has two intermediate nodes, the value of 3 and 4, respectively, we return to the second node.
Note:
Given the list of nodes is between 1 and 100.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/middle-of-the-linked-list

3 ways:

  1. Array: traverse the list, the node A are put into the array, and finally taking the intermediate node within the memory array under standard. time complexityO (N), N number of nodes in the linked list is. Space complexityO (N), I.e., array A spent space.
  2. Single pointer Method: traversal of the list twice. Traversing the list in the first count of the number of nodes N; returns to the second pass of the N / 2 node. time complexityO (N)Space complexityO (1)
  3. Finger speed: traversing the list together with two pointers slow and fast. slow time go one step further, fast moving twice. Then when the list reaches the end of the fast, slow necessarily in the middle. time complexityO (N)Space complexityO (1)

detail:

  1. / Represents division floating point, floating-point return result; // represents integer division, rounded down.
  2. Chain structure: node class (itself a pointer) is: data is data, next address (pointer) to the next node. head for the first node.
  3. c ++ has pointers, python pointer is not in the true sense, python pointers made a good package, everything is "object", all objects have a "variable" to it. This "variable" is the "pointer." Each variable is in python pointer.

Code Array Method 1:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        A = [head]
        while A[-1].next:
            A.append(A[-1].next)
        return A[len(A) // 2]

Code 2 single pointer Method:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        n, cur = 0, head
        while cur:
            n += 1
            cur = cur.next
        k, cur = 0, head
        while k < n // 2:
            k += 1
            cur = cur.next
        return cur

Code speed Finger 3:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        # 快慢指针
        fast = head
        slow = head
        while fast  and fast.next:
            slow=slow.next
            fast=fast.next.next
        return slow
        
Published 25 original articles · won praise 1 · views 284

Guess you like

Origin blog.csdn.net/weixin_43973433/article/details/105042676