LeetCode.876- intermediate node the list (Middle of the Linked List)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/codeMas/article/details/90286251

This is the first book of pleasure and delight 337 update, the first 361 Pian original

01 questions and look ready

LeetCode algorithm introduced today is the first title in the Easy level 206 title (overall title number is 876). Given nonempty single linked list having a head node, the intermediate node returns the list. If there are two intermediate nodes, the second intermediate node is returned. E.g:

Input: [1,2,3,4,5]
Output: This list of node 3 (sequence of: [3,4,5])
node returns a value of 3. (decision node into the sequence of [ 3,4,5]).
Please note that we returned a ListNode objects ans, like this:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, ans.next.next.next = NULL.


Input: [1,2,3,4,5,6]
Output: This list of node 4 (sequence of: [4,5,6])
due to the two intermediate nodes list has values of 3 and 4, we returns the second node.

Note :

  • A given number of nodes in the list will be between 1 and 100.

The use of problem-solving tools that eclipse, jdk version used is 1.8, the environment is win7 64-bit systems, and test using the Java language.

02 The first solution

Help to solve an array.

Traversing the linked list, into a List, a List of the intermediate element can be taken.

The time complexity of this solution is O (N), the spatial complexity is O (N).

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        List<ListNode> list = new ArrayList<ListNode>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        return list.get(list.size()/2);
    }
}

03 The second solution

Solutions used to speed pointers.

This problem may be understood to encounter problems of mathematics, a normal speed hand movement, a 2x speed fast forward pointer, a pointer to the fast when the end of the list, in an intermediate position definitely slow pointer list.

The time complexity of this solution is O (N), the spatial complexity is O (1).

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

04 Summary

Thematic algorithm has been more than six consecutive days, 206+ articles algorithm of feature articles, public Number dialog box, data structures and algorithms [reply], [algorithm], [either] in the data structure of a keyword to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!

Guess you like

Origin blog.csdn.net/codeMas/article/details/90286251