java - 按规则访问节点,删除链表中所有未访问的节点
问题描述
我试图根据链表中头指针的值遍历链表,因为一个节点包含两个值,即当前值和指向下一个的指针。我必须按照以下方式穿越
最初,您处于领先地位(值为 1 的节点)。因此,您移至第一个节点之后,即值为 2 的节点。
从节点 2 开始,您访问第二个节点,即值为 1 的节点。
以类似的方式,您转到倒数第二个节点(值 = 2)。
从那里开始,因为少于 2 个节点,您到达 NULL 并停止。
因此,访问的节点是 1、2、1 和 2
/*
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; next = null; }
}
*/
public class Solution {
satic void deleteUnvisitedNodes(ListNode head) {
ListNode current = head;
ListNode temp;
while(current != null){
int k = current.val;
temp = NthNode(current,k);
current.next = temp.next;
}
}
static ListNode NthNode(ListNode head, int k){
int count = 0;
ListNode current = head;
while(current !=null && current.next!=null){
if(count == k)
return current;
count++;
current = current.next;
}
return current;
}
public static void main(String[] args){
ListNode head = null;
head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(1);
head.next.next.next.next = new ListNode(2);
head.next.next.next.next.next = new ListNode(3);
ArrayList<ListNode> l = new ArrayList<>();
deleteUnvisitedNodes(head);
while(head != null){
l.add(head);
head = head.next;
}
System.out.print(l);
}
我收到运行时错误
解决方案
问题是您在删除方法中未访问的节点后没有继续前进,deleteUnvisitedNodes
这会导致无限次数的执行并因此超时。因此,在删除未访问的节点后,我们需要继续前进到该N-th
节点并继续该过程。
这是具有所需输出的工作代码:
class ListNode
{
public int val;
public ListNode next;
public ListNode(int x)
{ val = x; next = null; }
}
class Solution
{
void deleteUnvisitedNodes(ListNode head)
{
ListNode current = head;
ListNode temp;
while (current != null)
{
int k = current.val;
temp = NthNode(current,k);
current.next = temp;
current = current.next; // or current = temp // <-- Modified
}
}
ListNode NthNode(ListNode head, int k)
{
int count = 0;
ListNode current = head;
while (current != null && current.next != null)
{
if (count == k)
return current;
++count;
current = current.next;
}
return null; // <-- Modified
}
public static void main (String[] args)
{
ListNode a = new ListNode(1); ListNode b = new ListNode(2);
ListNode c = new ListNode(3); ListNode d = new ListNode(1);
ListNode e = new ListNode(2); ListNode f = new ListNode(3);
a.next = b; b.next = c; c.next = d; d.next = e; e.next = f;
ListNode head = a;
while (head != null)
{
System.out.print(head.val + " -> ");
head = head.next;
}System.out.println("null");
Solution sol = new Solution();
sol.deleteUnvisitedNodes( a );
head = a;
while (head != null)
{
System.out.print(head.val + " -> ");
head = head.next;
}System.out.println("null");
}
}
输出 :
1 -> 2 -> 3 -> 1 -> 2 -> 3 -> null
1 -> 2 -> 1 -> 2 -> null
如有任何疑问,请随时提出。
推荐阅读
- mongodb - 无法解决 MongoParseError: Invalid connection string
- docker - 连接到 localhost:5432 被拒绝。检查主机名和端口是否正确以及 postmaster 是否接受 TCP/IP 连接
- javascript - 如何在地图迭代中获取特定的对象键
- python - 如何使用 python-docx 搜索和替换 word 文档中的单词/文本
- java - 如何为 simpleDateFormat 创建正则表达式
- ngrx - 从效果中调用多个动作
- linux - 如何用 sed 命令替换一串特定字符
- firebase - 应用未激活时未调用 fcm onNotification
- javascript - 在 socket.io 中收集套接字返回的结果
- spring - How to use current exchange and queue name of rabbitmq with spring cloud stream rabbitmq