1171:从链表中删去总和值为零的连续节点
This commit is contained in:
parent
602b60c36d
commit
58f5cccd49
@ -0,0 +1,96 @@
|
||||
//给你一个链表的头节点 head,请你编写代码,反复删去链表中由 总和 值为 0 的连续节点组成的序列,直到不存在这样的序列为止。
|
||||
//
|
||||
// 删除完毕后,请你返回最终结果链表的头节点。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 你可以返回任何满足题目要求的答案。
|
||||
//
|
||||
// (注意,下面示例中的所有序列,都是对 ListNode 对象序列化的表示。)
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:head = [1,2,-3,3,1]
|
||||
//输出:[3,1]
|
||||
//提示:答案 [1,2,1] 也是正确的。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:head = [1,2,3,-3,4]
|
||||
//输出:[1,2,4]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:head = [1,2,3,-3,-2]
|
||||
//输出:[1]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 给你的链表中可能有 1 到 1000 个节点。
|
||||
// 对于链表中的每个节点,节点的值:-1000 <= node.val <= 1000.
|
||||
//
|
||||
// Related Topics 链表
|
||||
// 👍 122 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1171:从链表中删去总和值为零的连续节点
|
||||
public class RemoveZeroSumConsecutiveNodesFromLinkedList{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new RemoveZeroSumConsecutiveNodesFromLinkedList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode removeZeroSumSublists(ListNode head) {
|
||||
Map<Integer, ListNode> map = new HashMap<>();
|
||||
int sum = 0;
|
||||
ListNode temp = head;
|
||||
while (temp != null) {
|
||||
sum += temp.val;
|
||||
while (temp.next != null && temp.next.val == 0) {
|
||||
temp.next = temp.next.next;
|
||||
}
|
||||
if (sum == 0 || map.containsKey(sum)) {
|
||||
if (sum == 0) {
|
||||
head = temp.next;
|
||||
} else {
|
||||
map.get(sum).next = temp.next;
|
||||
}
|
||||
if (head != null) {
|
||||
map = new HashMap<>();
|
||||
temp = head;
|
||||
sum = head.val;
|
||||
} else {
|
||||
return head;
|
||||
}
|
||||
}
|
||||
map.put(sum, temp);
|
||||
temp = temp.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<p>给你一个链表的头节点 <code>head</code>,请你编写代码,反复删去链表中由 <strong>总和</strong> 值为 <code>0</code> 的连续节点组成的序列,直到不存在这样的序列为止。</p>
|
||||
|
||||
<p>删除完毕后,请你返回最终结果链表的头节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>你可以返回任何满足题目要求的答案。</p>
|
||||
|
||||
<p>(注意,下面示例中的所有序列,都是对 <code>ListNode</code> 对象序列化的表示。)</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,-3,3,1]
|
||||
<strong>输出:</strong>[3,1]
|
||||
<strong>提示:</strong>答案 [1,2,1] 也是正确的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,3,-3,4]
|
||||
<strong>输出:</strong>[1,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,3,-3,-2]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给你的链表中可能有 <code>1</code> 到 <code>1000</code> 个节点。</li>
|
||||
<li>对于链表中的每个节点,节点的值:<code>-1000 <= node.val <= 1000</code>.</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 122</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user