LeetCode 2181. 合并零之间的节点
原题链接
中等
作者:
autumn_0
,
2024-09-09 09:56:13
,
所有人可见
,
阅读 2
/**
* 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;
}
}