其他分享
首页 > 其他分享> > leetcode2181. 合并零之间的节点(mid)(281)

leetcode2181. 合并零之间的节点(mid)(281)

作者:互联网

合并零之间的节点


力扣链接

代码

class Solution {
    public ListNode mergeNodes(ListNode head) {
        ListNode newHead = new ListNode();
        ListNode t = head.next;
        ListNode t2 = newHead;
        int sum = 0;
        while (t != null) {
            sum += t.val;
            if (t.val == 0) {
                t2.val = sum;
                if (t.next != null) {
                    t2.next = new ListNode();
                    t2 = t2.next;
                }
                sum = 0;
            }
            t = t.next;
        }
        return newHead;
    }
}

标签:ListNode,val,sum,t2,mid,next,leetcode2181,281,newHead
来源: https://blog.csdn.net/qq_43478625/article/details/123058505