import java.util.HashMap;
import java.util.Scanner;
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class Main {
// 主函数,接受链表头节点并返回最终链表
public static ListNode removeZeroXorSegments(ListNode head) {
// 创建一个虚拟头节点,方便操作链表
ListNode dummy = new ListNode(0);
dummy.next = head;
// 创建一个哈希表来记录异或和的位置
HashMap
<Integer, ListNode
> map
= new HashMap
<>(); map.put(0, dummy); // 初始异或和为0的位置是虚拟头节点
int xorSum = 0; // 初始化异或和为0
ListNode curr = head; // 当前节点指针
// 遍历链表
while (curr != null) {
xorSum ^= curr.val; // 计算从头到当前节点的异或和
// 如果当前异或和在哈希表中存在,说明这段区间的异或和为0
if (map.containsKey(xorSum)) {
// 找到可以删除的节点段的前驱节点
ListNode prev = map.get(xorSum);
// 删除prev与curr之间的节点
ListNode temp = prev.next; // 需要被删除的节点
int tempXor = xorSum; // 临时变量存储异或和
// 移除中间的节点并更新哈希表
while (temp != curr) {
tempXor ^= temp.val; // 更新异或和
map.remove(tempXor); // 从哈希表中删除该异或和
temp = temp.next; // 移动到下一个节点
}
// 将前驱节点连接到当前节点的下一个位置,完成删除操作
prev.next = curr.next;
} else {
// 如果当前异或和不在哈希表中,记录该异或和及其对应的节点
map.put(xorSum, curr);
}
// 移动到下一个节点
curr = curr.next;
}
// 返回最终处理后的链表
return dummy.next;
}
// 从输入中读取链表
public static ListNode buildList
(String input
) { // 去除掉花括号和空格,然后按逗号分割
input = input.trim().replaceAll("[{}]", "");
if (input.isEmpty()) {
return null; // 如果输入为空,返回空链表
}
String[] values
= input.
split(","); // 按逗号分割输入的节点值 ListNode head
= new ListNode
(Integer.
parseInt(values
[0])); // 构造头节点 ListNode current = head; // 当前节点指针
for (int i = 1; i < values.length; i++) {
current.
next = new ListNode
(Integer.
parseInt(values
[i
])); // 构造下一个节点 current = current.next; // 移动到下一个节点
}
return head; // 返回构造好的链表
}
// 工具函数:打印链表
public static void printList(ListNode head) {
if (head == null) {
return;
}
ListNode curr = head;
while (curr != null) {
if (curr.next != null) {
}
curr = curr.next;
}
}
// 主函数
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in); // 创建Scanner对象接收输入 System.
out.
println("请输入链表节点的值,格式如:{1,3,2,1,4}"); String input
= scanner.
nextLine(); // 读取输入的链表节点值 ListNode head = buildList(input); // 构建链表
ListNode result = removeZeroXorSegments(head); // 删除异或和为0的段
printList(result); // 输出最终链表
}
}
aW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLlNjYW5uZXI7CgpjbGFzcyBMaXN0Tm9kZSB7CiAgICBpbnQgdmFsOwogICAgTGlzdE5vZGUgbmV4dDsKICAgIExpc3ROb2RlKGludCB2YWwpIHsKICAgICAgICB0aGlzLnZhbCA9IHZhbDsKICAgICAgICB0aGlzLm5leHQgPSBudWxsOwogICAgfQp9CgpwdWJsaWMgY2xhc3MgTWFpbiB7CgogICAgLy8g5Li75Ye95pWw77yM5o6l5Y+X6ZO+6KGo5aS06IqC54K55bm26L+U5Zue5pyA57uI6ZO+6KGoCiAgICBwdWJsaWMgc3RhdGljIExpc3ROb2RlIHJlbW92ZVplcm9Yb3JTZWdtZW50cyhMaXN0Tm9kZSBoZWFkKSB7CiAgICAgICAgLy8g5Yib5bu65LiA5Liq6Jma5ouf5aS06IqC54K577yM5pa55L6/5pON5L2c6ZO+6KGoCiAgICAgICAgTGlzdE5vZGUgZHVtbXkgPSBuZXcgTGlzdE5vZGUoMCk7CiAgICAgICAgZHVtbXkubmV4dCA9IGhlYWQ7CiAgICAgICAgCiAgICAgICAgLy8g5Yib5bu65LiA5Liq5ZOI5biM6KGo5p2l6K6w5b2V5byC5oiW5ZKM55qE5L2N572uCiAgICAgICAgSGFzaE1hcDxJbnRlZ2VyLCBMaXN0Tm9kZT4gbWFwID0gbmV3IEhhc2hNYXA8PigpOwogICAgICAgIG1hcC5wdXQoMCwgZHVtbXkpOyAgLy8g5Yid5aeL5byC5oiW5ZKM5Li6MOeahOS9jee9ruaYr+iZmuaLn+WktOiKgueCuQogICAgICAgIAogICAgICAgIGludCB4b3JTdW0gPSAwOyAgLy8g5Yid5aeL5YyW5byC5oiW5ZKM5Li6MAogICAgICAgIExpc3ROb2RlIGN1cnIgPSBoZWFkOyAgLy8g5b2T5YmN6IqC54K55oyH6ZKICiAgICAgICAgCiAgICAgICAgLy8g6YGN5Y6G6ZO+6KGoCiAgICAgICAgd2hpbGUgKGN1cnIgIT0gbnVsbCkgewogICAgICAgICAgICB4b3JTdW0gXj0gY3Vyci52YWw7ICAvLyDorqHnrpfku47lpLTliLDlvZPliY3oioLngrnnmoTlvILmiJblkowKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIOWmguaenOW9k+WJjeW8guaIluWSjOWcqOWTiOW4jOihqOS4reWtmOWcqO+8jOivtOaYjui/meauteWMuumXtOeahOW8guaIluWSjOS4ujAKICAgICAgICAgICAgaWYgKG1hcC5jb250YWluc0tleSh4b3JTdW0pKSB7CiAgICAgICAgICAgICAgICAvLyDmib7liLDlj6/ku6XliKDpmaTnmoToioLngrnmrrXnmoTliY3pqbHoioLngrkKICAgICAgICAgICAgICAgIExpc3ROb2RlIHByZXYgPSBtYXAuZ2V0KHhvclN1bSk7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIC8vIOWIoOmZpHByZXbkuI5jdXJy5LmL6Ze055qE6IqC54K5CiAgICAgICAgICAgICAgICBMaXN0Tm9kZSB0ZW1wID0gcHJldi5uZXh0OyAgLy8g6ZyA6KaB6KKr5Yig6Zmk55qE6IqC54K5CiAgICAgICAgICAgICAgICBpbnQgdGVtcFhvciA9IHhvclN1bTsgIC8vIOS4tOaXtuWPmOmHj+WtmOWCqOW8guaIluWSjAogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAvLyDnp7vpmaTkuK3pl7TnmoToioLngrnlubbmm7TmlrDlk4jluIzooagKICAgICAgICAgICAgICAgIHdoaWxlICh0ZW1wICE9IGN1cnIpIHsKICAgICAgICAgICAgICAgICAgICB0ZW1wWG9yIF49IHRlbXAudmFsOyAgLy8g5pu05paw5byC5oiW5ZKMCiAgICAgICAgICAgICAgICAgICAgbWFwLnJlbW92ZSh0ZW1wWG9yKTsgIC8vIOS7juWTiOW4jOihqOS4reWIoOmZpOivpeW8guaIluWSjAogICAgICAgICAgICAgICAgICAgIHRlbXAgPSB0ZW1wLm5leHQ7ICAvLyDnp7vliqjliLDkuIvkuIDkuKroioLngrkKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgLy8g5bCG5YmN6amx6IqC54K56L+e5o6l5Yiw5b2T5YmN6IqC54K555qE5LiL5LiA5Liq5L2N572u77yM5a6M5oiQ5Yig6Zmk5pON5L2cCiAgICAgICAgICAgICAgICBwcmV2Lm5leHQgPSBjdXJyLm5leHQ7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAvLyDlpoLmnpzlvZPliY3lvILmiJblkozkuI3lnKjlk4jluIzooajkuK3vvIzorrDlvZXor6XlvILmiJblkozlj4rlhbblr7nlupTnmoToioLngrkKICAgICAgICAgICAgICAgIG1hcC5wdXQoeG9yU3VtLCBjdXJyKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgLy8g56e75Yqo5Yiw5LiL5LiA5Liq6IqC54K5CiAgICAgICAgICAgIGN1cnIgPSBjdXJyLm5leHQ7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIC8vIOi/lOWbnuacgOe7iOWkhOeQhuWQjueahOmTvuihqAogICAgICAgIHJldHVybiBkdW1teS5uZXh0OwogICAgfQoKICAgIC8vIOS7jui+k+WFpeS4reivu+WPlumTvuihqAogICAgcHVibGljIHN0YXRpYyBMaXN0Tm9kZSBidWlsZExpc3QoU3RyaW5nIGlucHV0KSB7CiAgICAgICAgLy8g5Y676Zmk5o6J6Iqx5ous5Y+35ZKM56m65qC877yM54S25ZCO5oyJ6YCX5Y+35YiG5YmyCiAgICAgICAgaW5wdXQgPSBpbnB1dC50cmltKCkucmVwbGFjZUFsbCgiW3t9XSIsICIiKTsKICAgICAgICBpZiAoaW5wdXQuaXNFbXB0eSgpKSB7CiAgICAgICAgICAgIHJldHVybiBudWxsOyAgLy8g5aaC5p6c6L6T5YWl5Li656m677yM6L+U5Zue56m66ZO+6KGoCiAgICAgICAgfQogICAgICAgIAogICAgICAgIFN0cmluZ1tdIHZhbHVlcyA9IGlucHV0LnNwbGl0KCIsIik7ICAvLyDmjInpgJflj7fliIblibLovpPlhaXnmoToioLngrnlgLwKICAgICAgICBMaXN0Tm9kZSBoZWFkID0gbmV3IExpc3ROb2RlKEludGVnZXIucGFyc2VJbnQodmFsdWVzWzBdKSk7ICAvLyDmnoTpgKDlpLToioLngrkKICAgICAgICBMaXN0Tm9kZSBjdXJyZW50ID0gaGVhZDsgIC8vIOW9k+WJjeiKgueCueaMh+mSiAogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgdmFsdWVzLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIGN1cnJlbnQubmV4dCA9IG5ldyBMaXN0Tm9kZShJbnRlZ2VyLnBhcnNlSW50KHZhbHVlc1tpXSkpOyAgLy8g5p6E6YCg5LiL5LiA5Liq6IqC54K5CiAgICAgICAgICAgIGN1cnJlbnQgPSBjdXJyZW50Lm5leHQ7ICAvLyDnp7vliqjliLDkuIvkuIDkuKroioLngrkKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGhlYWQ7ICAvLyDov5Tlm57mnoTpgKDlpb3nmoTpk77ooagKICAgIH0KCiAgICAvLyDlt6Xlhbflh73mlbDvvJrmiZPljbDpk77ooagKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBwcmludExpc3QoTGlzdE5vZGUgaGVhZCkgewogICAgICAgIGlmIChoZWFkID09IG51bGwpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ7fSIpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIExpc3ROb2RlIGN1cnIgPSBoZWFkOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnQoInsiKTsKICAgICAgICB3aGlsZSAoY3VyciAhPSBudWxsKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoY3Vyci52YWwpOwogICAgICAgICAgICBpZiAoY3Vyci5uZXh0ICE9IG51bGwpIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoIiwiKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBjdXJyID0gY3Vyci5uZXh0OwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIn0iKTsKICAgIH0KCiAgICAvLyDkuLvlh73mlbAKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOyAgLy8g5Yib5bu6U2Nhbm5lcuWvueixoeaOpeaUtui+k+WFpQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigi6K+36L6T5YWl6ZO+6KGo6IqC54K555qE5YC877yM5qC85byP5aaC77yaezEsMywyLDEsNH0iKTsKICAgICAgICBTdHJpbmcgaW5wdXQgPSBzY2FubmVyLm5leHRMaW5lKCk7ICAvLyDor7vlj5bovpPlhaXnmoTpk77ooajoioLngrnlgLwKICAgICAgICBMaXN0Tm9kZSBoZWFkID0gYnVpbGRMaXN0KGlucHV0KTsgIC8vIOaehOW7uumTvuihqAogICAgICAgIExpc3ROb2RlIHJlc3VsdCA9IHJlbW92ZVplcm9Yb3JTZWdtZW50cyhoZWFkKTsgIC8vIOWIoOmZpOW8guaIluWSjOS4ujDnmoTmrrUKICAgICAgICBwcmludExpc3QocmVzdWx0KTsgIC8vIOi+k+WHuuacgOe7iOmTvuihqAogICAgfQp9Cg==