当前位置:网站首页>876. Intermediate node of linked list

876. Intermediate node of linked list

2022-07-04 19:21:00 charlsdm

  1. The middle node of a list
    Given a header node as head The non empty single chain table of , Returns the middle node of the linked list .

If there are two intermediate nodes , Then return to the second intermediate node .

Example 1:

Input :[1,2,3,4,5]
Output : Nodes in this list 3 ( Serialization form :[3,4,5])
The returned node value is 3 . ( The evaluation system expresses the serialization of this node as follows [3,4,5]).
Be careful , We returned one ListNode Object of type ans, such :
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, as well as ans.next.next.next = NULL.
Example 2:

Input :[1,2,3,4,5,6]
Output : Nodes in this list 4 ( Serialization form :[4,5,6])
Because the list has two intermediate nodes , Values, respectively 3 and 4, Let's go back to the second node .

Tips :

The number of nodes in a given list is between 1 and 100 Between .

Attached below is my AC Code

  public partial class Solution
        {
    
            public ListNode MiddleNode(ListNode head)
            {
    
                ListNode[] listNodes = new ListNode[100];
                int index = 0;
                
                while(head!=null)
                {
    
                    listNodes[index] = head;
                
                    head = head.next;
                    index = index + 1;
                }
               
                return listNodes[(index)/2];
            }
        }
原网站

版权声明
本文为[charlsdm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041739222775.html