列表

详情


2181. 合并零之间的节点

给你一个链表的头节点 head ,该链表包含由 0 分隔开的一连串整数。链表的 开端末尾 的节点都满足 Node.val == 0

对于每两个相邻的 0 ,请你将它们之间的所有节点合并成一个节点,其值是所有已合并节点的值之和。然后将所有 0 移除,修改后的链表不应该含有任何 0

 返回修改后链表的头节点 head

 

示例 1:

输入:head = [0,3,1,0,4,5,2,0]
输出:[4,11]
解释:
上图表示输入的链表。修改后的链表包含:
- 标记为绿色的节点之和:3 + 1 = 4
- 标记为红色的节点之和:4 + 5 + 2 = 11

示例 2:

输入:head = [0,1,0,3,0,2,2,0]
输出:[1,3,4]
解释:
上图表示输入的链表。修改后的链表包含:
- 标记为绿色的节点之和:1 = 1
- 标记为红色的节点之和:3 = 3
- 标记为黄色的节点之和:2 + 2 = 4

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* mergeNodes(ListNode* head) { } };

cpp 解法, 执行用时: 495 ms, 内存消耗: 252 MB, 提交时间: 2024-09-09 09:06:14

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
        auto tail = head;
        for (auto cur = head->next; cur->next; cur = cur->next) {
            if (cur->val) {
                tail->val += cur->val;
            } else {
                tail = tail->next;
                tail->val = 0;
            }
        }
        // 注:这里没有 delete 剩余节点,可以自行补充
        tail->next = nullptr;
        return head;
    }
};

java 解法, 执行用时: 6 ms, 内存消耗: 78.1 MB, 提交时间: 2024-09-09 09:05:57

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeNodes(ListNode head) {
        ListNode tail = head;
        for (ListNode cur = head.next; cur.next != null; cur = cur.next) {
            if (cur.val != 0) {
                tail.val += cur.val;
            } else {
                tail = tail.next;
                tail.val = 0;
            }
        }
        tail.next = null;
        return head;
    }
}

javascript 解法, 执行用时: 418 ms, 内存消耗: 89.1 MB, 提交时间: 2024-09-09 09:05:36

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var mergeNodes = function(head) {
    let tail = head;
    for (let cur = head.next; cur.next; cur = cur.next) {
        if (cur.val) {
            tail.val += cur.val;
        } else {
            tail = tail.next;
            tail.val = 0;
        }
    }
    tail.next = null;
    return head;
};

golang 解法, 执行用时: 280 ms, 内存消耗: 13.6 MB, 提交时间: 2022-11-10 17:39:28

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeNodes(head *ListNode) *ListNode {
	ans := head
	for node, sum := head.Next, 0; node != nil; node = node.Next {
		if node.Val != 0 {
			sum += node.Val
		} else {
			ans.Next.Val = sum // 原地修改
			ans = ans.Next
			sum = 0
		}
	}
	ans.Next = nil
	return head.Next
}

python3 解法, 执行用时: 2108 ms, 内存消耗: 102.9 MB, 提交时间: 2022-11-10 17:32:24

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = tail = ListNode()
        total = 0
        cur = head.next

        while cur:
            if cur.val == 0:
                node = ListNode(total)
                tail.next = node
                tail = tail.next
                total = 0
            else:
                total += cur.val
            
            cur = cur.next
        
        return dummy.next

上一题