如何将LinkedList从中间节点打印到最后一个节点?
我正在尝试解决leetcode问题 876。链接列表的中间:
给定单链接列表的头部,返回链接列表的中间节点。
如果有两个中间节点,请返回第二个中间节点节点。
输入: head = [1,2,3,4,5]
输出: [3,4,5]
说明:列表的中间节点是节点3。
我正在尝试在VS代码上运行我的代码,并尝试从用户获取输入列表,并从中间节点从中间节点打印链接列表最后一个节点。
我无法打印所需的链接列表,而是我的代码打印了列表的最后一个元素。我已经看到了与链接列表有关的其他答案,但所有的答案仍然不知道我在做什么错。您能帮我解决这个问题吗?
这是代码:
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 ListNode middleNode(ListNode head) {
ListNode slow=head, fast=head;
while( fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
主类:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ListNode ll= new ListNode();
System.out.print("Enter the size of linked list: ");
int size = sc.nextInt();
System.out.println("Enter the elements of linked list: ");
for(int i =1 ; i<=size ; i++){
ll = new ListNode(sc.nextInt());
}
sc.close();
Solution ob = new Solution();
ListNode res=ob.middleNode(ll);
System.out.println("Linked list starting from the: ");
while(res != null) {
System.out.print(res.val + " ");
res = res.next;
}
System.out.println();
}
}
I'm trying to solve the LeetCode problem 876. Middle of the Linked List:
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
I'm trying to run my code on VS Code and trying to take the input list from the user and printing the linked list from the middle node till the last node.
I'm not able to print the desired linked list, instead my code prints the last element of the list. I've seen other answers related to linked list and all but still don't know what I'm doing wrong. Can you help me in solving this?
Here's the code:
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 to find the middle of the list and return the list node from the middle:
public class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head, fast=head;
while( fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
Main class:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ListNode ll= new ListNode();
System.out.print("Enter the size of linked list: ");
int size = sc.nextInt();
System.out.println("Enter the elements of linked list: ");
for(int i =1 ; i<=size ; i++){
ll = new ListNode(sc.nextInt());
}
sc.close();
Solution ob = new Solution();
ListNode res=ob.middleNode(ll);
System.out.println("Linked list starting from the: ");
while(res != null) {
System.out.print(res.val + " ");
res = res.next;
}
System.out.println();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于,在调用新操作时,LL将被重置,并且您只能获得最后一个节点,请参见Privous Code中的注释,以下代码显示了一个正确的示例之一。
the problem is that when calling the new operation, ll will be resetted, and you only get the last node, see the comment in prevous code, the following code show one of the correct example.
如注释中所述,您的问题是您将
ll
传递到MiddleNode
方法中,但是ll
正在引用您的最后一个节点,而您的列表为只有一个节点长,因为您从不使用next
在循环内的变量,而是完全替换了节点ll = new ListNode(sc.nextint());
。解决方案非常简单,在创建链接列表时,我们需要保留两个参考(我重命名了变量,以使它们更有意义):
然后,当我们插入下一个项目时,我们使用
PROVEN.NEXT.NEXT = new ListNode < /code> not
head = new ListNode(sc.nextint());
,请注意我们如何使用tail.next.next.next.next
nothead 而不是
tail
:完整的工作主方法:
以及
1 2 3 4 5
的输入的样本输出IS:As mentioned in comments, your issue is that you pass
ll
into yourmiddleNode
method, butll
is referencing to your last node and your list is only one node long because you never make use of thenext
variable inside your loop, instead you replace the node entirelyll = new ListNode(sc.nextInt());
.The solution is quite simple, we need to keep two references when creating the linked list (I have renamed the variables so that they make more sense):
Then when we insert the next item we use
previous.next = new ListNode
NOThead = new ListNode(sc.nextInt());
, note how we assign the next node usingtail.next
nothead
and nottail
:The complete working main method:
And the sample output from an input of
1 2 3 4 5
is: