fork download
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. class ListNode {
  5. int val;
  6. ListNode next;
  7. ListNode(int val) {
  8. this.val = val;
  9. this.next = null;
  10. }
  11. }
  12.  
  13. public class Main {
  14.  
  15. // 主函数,接受链表头节点并返回最终链表
  16. public static ListNode removeZeroXorSegments(ListNode head) {
  17. // 创建一个虚拟头节点,方便操作链表
  18. ListNode dummy = new ListNode(0);
  19. dummy.next = head;
  20.  
  21. // 创建一个哈希表来记录异或和的位置
  22. HashMap<Integer, ListNode> map = new HashMap<>();
  23. map.put(0, dummy); // 初始异或和为0的位置是虚拟头节点
  24.  
  25. int xorSum = 0; // 初始化异或和为0
  26. ListNode curr = head; // 当前节点指针
  27.  
  28. // 遍历链表
  29. while (curr != null) {
  30. xorSum ^= curr.val; // 计算从头到当前节点的异或和
  31.  
  32. // 如果当前异或和在哈希表中存在,说明这段区间的异或和为0
  33. if (map.containsKey(xorSum)) {
  34. // 找到可以删除的节点段的前驱节点
  35. ListNode prev = map.get(xorSum);
  36.  
  37. // 删除prev与curr之间的节点
  38. ListNode temp = prev.next; // 需要被删除的节点
  39. int tempXor = xorSum; // 临时变量存储异或和
  40.  
  41. // 移除中间的节点并更新哈希表
  42. while (temp != curr) {
  43. tempXor ^= temp.val; // 更新异或和
  44. map.remove(tempXor); // 从哈希表中删除该异或和
  45. temp = temp.next; // 移动到下一个节点
  46. }
  47.  
  48. // 将前驱节点连接到当前节点的下一个位置,完成删除操作
  49. prev.next = curr.next;
  50. } else {
  51. // 如果当前异或和不在哈希表中,记录该异或和及其对应的节点
  52. map.put(xorSum, curr);
  53. }
  54.  
  55. // 移动到下一个节点
  56. curr = curr.next;
  57. }
  58.  
  59. // 返回最终处理后的链表
  60. return dummy.next;
  61. }
  62.  
  63. // 从输入中读取链表
  64. public static ListNode buildList(String input) {
  65. // 去除掉花括号和空格,然后按逗号分割
  66. input = input.trim().replaceAll("[{}]", "");
  67. if (input.isEmpty()) {
  68. return null; // 如果输入为空,返回空链表
  69. }
  70.  
  71. String[] values = input.split(","); // 按逗号分割输入的节点值
  72. ListNode head = new ListNode(Integer.parseInt(values[0])); // 构造头节点
  73. ListNode current = head; // 当前节点指针
  74. for (int i = 1; i < values.length; i++) {
  75. current.next = new ListNode(Integer.parseInt(values[i])); // 构造下一个节点
  76. current = current.next; // 移动到下一个节点
  77. }
  78. return head; // 返回构造好的链表
  79. }
  80.  
  81. // 工具函数:打印链表
  82. public static void printList(ListNode head) {
  83. if (head == null) {
  84. System.out.println("{}");
  85. return;
  86. }
  87. ListNode curr = head;
  88. System.out.print("{");
  89. while (curr != null) {
  90. System.out.print(curr.val);
  91. if (curr.next != null) {
  92. System.out.print(",");
  93. }
  94. curr = curr.next;
  95. }
  96. System.out.println("}");
  97. }
  98.  
  99. // 主函数
  100. public static void main(String[] args) {
  101. Scanner scanner = new Scanner(System.in); // 创建Scanner对象接收输入
  102. System.out.println("请输入链表节点的值,格式如:{1,3,2,1,4}");
  103. String input = scanner.nextLine(); // 读取输入的链表节点值
  104. ListNode head = buildList(input); // 构建链表
  105. ListNode result = removeZeroXorSegments(head); // 删除异或和为0的段
  106. printList(result); // 输出最终链表
  107. }
  108. }
  109.  
Success #stdin #stdout 0.16s 54660KB
stdin
{2,3,4,5}
stdout
请输入链表节点的值,格式如:{1,3,2,1,4}
{}