题解 | #合并两群能量值#
合并两群能量值
https://www.nowcoder.com/practice/d728938f66ac44b5923d4f2e185667ec
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
public ListNode mergeEnergyValues (ListNode l1, ListNode l2) {
ListNode cur1 = l1;
ListNode cur2 = l2;
ListNode head = new ListNode(0);
ListNode cur = head;
while(cur1 != null) {
ListNode t1 = cur1;
ListNode t2 = cur2;
cur1 = cur1.next;
cur2 = cur2.next;
if(t1.val >= t2.val) {
cur.next = t1;
cur.next.next = t2;
cur = cur.next.next;
} else {
cur.next = t2;
cur.next.next = t1;
cur = cur.next.next;
}
}
return head.next;
}
}

