/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode(-1);
ListNode rcur = result;
ListNode cur1 = l1;
ListNode cur2 = l2;
int carry = 0;
while (cur1 != null && cur2 != null) {
int cur = cur1.val + cur2.val + carry;
rcur.next = new ListNode(cur % 10);
carry = cur >= 10 ? cur / 10 : 0;
cur1 = cur1.next;
cur2 = cur2.next;
rcur = rcur.next;
}
if (cur1 != null) {
rcur.next = cur1;
} else {
rcur.next = cur2;
}
return result.next;
}
}
Wrong answer
digit수가 다를 경우를 생각 X
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode(-1);
ListNode rcur = result;
ListNode cur1 = l1;
ListNode cur2 = l2;
int carry = 0;
while (cur1 != null && cur2 != null) {
int cur = cur1.val + cur2.val + carry;
rcur.next = new ListNode(cur % 10);
carry = cur >= 10 ? cur / 10 : 0;
cur1 = cur1.next;
cur2 = cur2.next;
rcur = rcur.next;
}
while (cur1 != null) {
int c1 = cur1.val + carry;
rcur.next = new ListNode(c1 % 10);
carry = c1 >= 10 ? c1 / 10 : 0;
cur1 = cur1.next;
rcur = rcur.next;
}
while (cur2 != null) {
int c2 = cur2.val + carry;
rcur.next = new ListNode(c2 % 10);
carry = c2 >= 10 ? c2 / 10 : 0;
cur2 = cur2.next;
rcur = rcur.next;
}
if (carry != 0) {
rcur.next = new ListNode(carry);
}
return result.next;
}
}
혼자서 풀어서 너무 뿌듯!!
저번에 풀었을때에는 루션이를 봤었네요^^..
같은 아이디어인데 훨씬 깔끔하게 쓰여짐
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}
/**
* 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 oddEvenList(ListNode head) {
ListNode evens = new ListNode(-1);
ListNode odds = new ListNode(-1);
ListNode oc = odds;
ListNode ec = evens;
ListNode cur = head;
boolean isOdd = true;
while (cur != null) {
if (isOdd) {
odds.next = cur;
odds = odds.next;
} else {
evens.next = cur;
evens = evens.next;
}
isOdd = ! isOdd;
cur = cur.next;
}
evens.next = null;
odds.next = ec.next;
return oc.next;
}
}
뿌듯뿌듯~~
이거 처음에 풀었을떄는 파이선으로 풀었었네요;
지금은 파이선 하나도 못하겠다는점^_^
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode cura = headA;
ListNode curb = headB;
while (cura != curb) {
cura = cura == null ? headB : cura.next;
curb = curb == null? headA : curb.next;
}
return cura;
}
}
아직도 충격적인 코드..
냅다 외운게 효과가 있네요^__^