【leetcode】【876】【链表的中间结点】

发布时间 2023-07-02 19:50:29作者: laolang2016

c++

第一个方法

#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>

// Definition for singly-linked list.
struct ListNode {
    int       val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

static void print_list(ListNode* list) {
    if (nullptr == list) {
        return;
    }
    ListNode* curr = list;
    while (nullptr != curr) {
        std::cout << curr->val << " ";
        curr = curr->next;
    }
    std::cout << std::endl;
}

#define PRINT_LOG

class Solution {
public:
    // 计算长度
    size_t length(ListNode* head) {
        if (nullptr == head) {
            return 0;
        }
        size_t length = 0;
        auto   cursor = head;
        while (nullptr != cursor) {
            cursor = cursor->next;
            length++;
        }
        return length;
    }

    ListNode* middleNode(ListNode* head) {
        auto len = length(head);
        if (0 == len) {
            return nullptr;
        }

        auto   index  = len / 2;
        size_t i      = 0;
        auto   cursor = head;
        while (i < index) {
            cursor = cursor->next;
            i++;
        }

        return cursor;
    }
};

static void fill_list(ListNode* list, std::vector<int> data) {
    ListNode* curr = list;
    for (auto i = 0; i < data.size(); i++) {
        curr->val = data[i];
        if (i < data.size() - 1) {
            curr->next = new ListNode();
            curr       = curr->next;
        }
    }
}

void test_001() {
    std::cout << "==== test 001:" << std::endl;
    auto l1 = std::make_unique<ListNode>();
    fill_list(l1.get(), {1, 2, 3, 4, 5});

    auto solution = std::make_unique<Solution>();
    auto ret      = solution->middleNode(l1.get());
    std::cout << "ret:" << ret->val << std::endl;
}

void test_002() {
    std::cout << "==== test 002:" << std::endl;
    auto l1 = std::make_unique<ListNode>();
    fill_list(l1.get(), {1, 2, 3, 4, 5, 6});

    auto solution = std::make_unique<Solution>();
    auto ret      = solution->middleNode(l1.get());
    std::cout << "ret:" << ret->val << std::endl;
}

int main() {
    system("chcp 65001");
    test_001();
    test_002();
    std::cout << "Hello World!" << std::endl;
}

java

第一个方法

ListNode.java

package com.laolang.leetcode;
 
public class ListNode {
 
    public int val;
    public ListNode next;
 
    public ListNode() {
    }
 
    public ListNode(int val) {
        this.val = val;
    }
 
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

CommonTest.java

package com.laolang.leetcode;

import java.util.List;
import java.util.Objects;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

public class CommonTest {

    private Solution solution;
    private ListNode list1;


    private void fillList(ListNode list, List<Integer> data) {
        ListNode node = list;
        for (int i = 0; i < data.size(); i++) {
            node.val = data.get(i);
            if (i < data.size() - 1) {
                node.next = new ListNode();
                node = node.next;
            }
        }
    }

    @BeforeClass
    public void beforeClass() {
        solution = new Solution();
        list1 = new ListNode();
        fillList(list1, Lists.newArrayList(1, 2, 3, 4, 5, 6));
    }

    private void printList(ListNode list) {
        if (Objects.isNull(list)) {
            return;
        }
        ListNode curr = list;
        while (Objects.nonNull(curr)) {
            System.out.print(curr.val);
            System.out.print(" ");
            curr = curr.next;
        }
        System.out.println();
    }

    @Test
    public void testOne() {
        printList(list1);
        ListNode ret = solution.middleNode(list1);
        System.out.println(ret.val);
    }
}

第一个方法

package com.laolang.leetcode;

/**
 * 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; }
 * }
 */
public class Solution {

    public int length(ListNode head) {
        if (null == head) {
            return 0;
        }
        int length = 0;
        ListNode cursor = head;
        while (null != cursor) {
            cursor = cursor.next;
            length++;
        }
        return length;
    }


    public ListNode middleNode(ListNode head) {
        int len = length(head);
        if (0 == len) {
            return null;
        }

        int index = len / 2;
        int i = 0;
        ListNode cursor = head;
        while (i < index) {
            cursor = cursor.next;
            i++;
        }

        return cursor;
    }
}