首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:需要了解LinkNode构造函数和支持函数

问Java:需要了解LinkNode构造函数和支持函数
EN

Stack Overflow用户
提问于 2020-07-09 10:42:55
回答 1查看 43关注 0票数 1

我已经开始研究Java中的LinkedList问题。我遇到了LinkNode,这让我感到困惑。我在下面的leetcode:https://leetcode.com/problems/odd-even-linked-list/discuss/78079/Simple-O(N)-time-O(1)-space-Java-solution找到了问题的解决方案。我想了解更多关于LinkNode的信息。另外,执行下面的函数需要什么样的支持函数?

代码语言:javascript
复制
public class Solution {
public ListNode oddEvenList(ListNode head) {
    if (head != null) {
    
        ListNode odd = head, even = head.next, evenHead = even; 
    
        while (even != null && even.next != null) {
            odd.next = odd.next.next; 
            even.next = even.next.next; 
            odd = odd.next;
            even = even.next;
        }
        odd.next = evenHead; 
    }
    return head;
}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-09 10:49:49

它已经由LeetCode在他们的模板中定义了,你不需要编写代码:

代码语言:javascript
复制
/**
 * 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; }
 * }
 */
class Solution {
    public ListNode oddEvenList(ListNode head) {
        
    }
}

你的代码很好,它会通过:

代码语言:javascript
复制
class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head != null) {
            ListNode odd = head;
            ListNode even = head.next;
            ListNode evenHead = even;

            while (even != null && even.next != null) {
                odd.next = odd.next.next;
                even.next = even.next.next;
                odd = odd.next;
                even = even.next;
            }

            odd.next = evenHead;
        }

        return head;
    }
}

例如,如果您想在本地测试,可以创建一个名为b.java的文件

代码语言:javascript
复制
import java.util.*;
import javafx.util.*;

class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head != null) {
            ListNode odd = head;
            ListNode even = head.next;
            ListNode evenHead = even;

            while (even != null && even.next != null) {
                odd.next = odd.next.next;
                even.next = even.next.next;
                odd = odd.next;
                even = even.next;
            }

            odd.next = evenHead;
        }

        return head;
    }
}


class b {
    public static void main(String[] args) {
        Solution object = new Solution();
        System.out.println(object);
    }
}


class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

然后写你的代码,不管你想怎么测试它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62806574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档