-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday31.java
More file actions
60 lines (47 loc) · 1.35 KB
/
Copy pathday31.java
File metadata and controls
60 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//ques1:203. Remove Linked List Elements
//link:https://leetcode.com/problems/remove-linked-list-elements/description/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null){
return head;
}
while(head!=null && head.val==val ){
head=head.next;
}
ListNode cur=head;
while(cur!=null && cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}
else{
cur=cur.next;
}
}
return head;
}
}
//ques2:2. Add Two Numbers
//link:https://leetcode.com/problems/add-two-numbers/description/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode current = dummy;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int sum = carry;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
carry = sum / 10;
ListNode node = new ListNode(sum % 10);
current.next = node;
current = current.next;
}
return dummy.next;
}
}