package test;
public class Test {
public static void main(String[] args) {
ListNode<Integer> first = null;
ListNode<Integer> pre = null;//这里面的pre就是个工具人。
//首先我先创建循环链表
for (int i = 1; i <= 41 ; i++) {
if (i==1){
first = new ListNode<>(i);
pre = first;
continue;
}
ListNode node = new ListNode<>(i);
pre.next = node;
pre = node;//这里面已经创建可了41个节点了。
if (i == 41){
pre.next =first;
}
}//构建完成了循环链表
int count = 0;
ListNode<Integer> cur = first;
ListNode<Integer> before = null;
while(cur != cur.next){
count++;//开始自杀
if (count == 3){
System.out.print(cur.val + ",");
before.next = cur.next;//删除节点
cur = cur.next;//删除节点后,接节点向后移动,before就不用了,因为他还是在那个位置。
count = 0;
}else{
before = cur;
cur = cur.next;
}
}
System.out.println(cur);
}
}
约瑟夫问题
最新推荐文章于 2025-07-30 16:47:17 发布