Skip to content

Commit 3698772

Browse files
committed
java 2/19/21/23/24/25
1 parent bca1610 commit 3698772

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

java/_002AddTwoNumbers.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
3+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
4+
* <p>
5+
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
6+
* Output: 7 -> 0 -> 8
7+
* <p>
8+
* Created by drfish on 16/04/2017.
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
*/
14+
class ListNode {
15+
int val;
16+
ListNode next;
17+
18+
ListNode(int x) {
19+
val = x;
20+
}
21+
}
22+
23+
public class _002AddTwoNumbers {
24+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
25+
ListNode result = new ListNode(0);
26+
ListNode cur = result;
27+
while (l1 != null || l2 != null) {
28+
cur.val += addTwoNodes(l1, l2);
29+
if (cur.val >= 10) {
30+
cur.val -= 10;
31+
cur.next = new ListNode(1);
32+
} else {
33+
if (l1 != null && l1.next != null || l2 != null && l2.next != null)
34+
cur.next = new ListNode(0);
35+
}
36+
cur = cur.next;
37+
if (l1 != null)
38+
l1 = l1.next;
39+
if (l2 != null)
40+
l2 = l2.next;
41+
}
42+
return result;
43+
}
44+
45+
private int addTwoNodes(ListNode n1, ListNode n2) {
46+
if (n1 == null)
47+
return n2.val;
48+
if (n2 == null)
49+
return n1.val;
50+
return n1.val + n2.val;
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Given a linked list, remove the nth node from the end of list and return its head.
3+
* For example,
4+
* Given linked list: 1->2->3->4->5, and n = 2.
5+
* After removing the second node from the end, the linked list becomes 1->2->3->5.
6+
* <p>
7+
* Note:
8+
* Given n will always be valid.
9+
* Try to do this in one pass.
10+
* <p>
11+
* Created by drfish on 16/04/2017.
12+
*/
13+
14+
public class _019RemoveNthNodeFromEndOfList {
15+
public ListNode removeNthFromEnd(ListNode head, int n) {
16+
ListNode dummy = new ListNode(-1);
17+
dummy.next = head;
18+
ListNode fast = dummy;
19+
ListNode slow = dummy;
20+
for (int i = 0; i < n; i++)
21+
fast = fast.next;
22+
while (fast.next != null) {
23+
fast = fast.next;
24+
slow = slow.next;
25+
}
26+
slow.next = slow.next.next;
27+
return dummy.next;
28+
}
29+
}

java/_021MergeTwoSortedLists.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
3+
* <p>
4+
* Created by drfish on 16/04/2017.
5+
*/
6+
public class _021MergeTwoSortedLists {
7+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
8+
ListNode dummy = new ListNode(-1);
9+
ListNode head = dummy;
10+
while (l1 != null && l2 != null) {
11+
if (l1.val < l2.val) {
12+
dummy.next = l1;
13+
l1 = l1.next;
14+
} else {
15+
dummy.next = l2;
16+
l2 = l2.next;
17+
}
18+
dummy = dummy.next;
19+
}
20+
if (l1 == null)
21+
dummy.next = l2;
22+
if (l2 == null)
23+
dummy.next = l1;
24+
return head.next;
25+
}
26+
}

java/_023MergekSortedLists.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.PriorityQueue;
2+
3+
/**
4+
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
5+
* <p>
6+
* Created by drfish on 16/04/2017.
7+
*/
8+
public class _023MergekSortedLists {
9+
public ListNode mergeKLists(ListNode[] lists) {
10+
PriorityQueue<ListNode> nodesHeap = new PriorityQueue<ListNode>((n1, n2) -> (n1.val - n2.val));
11+
for (ListNode node : lists) {
12+
if (node != null)
13+
nodesHeap.add(node);
14+
}
15+
ListNode dummy = new ListNode(-1);
16+
ListNode head = dummy;
17+
while (!nodesHeap.isEmpty()) {
18+
ListNode node = nodesHeap.poll();
19+
if (node.next != null)
20+
nodesHeap.add(node.next);
21+
dummy.next = node;
22+
dummy = dummy.next;
23+
}
24+
return head.next;
25+
}
26+
}

java/_024SwapNodesInPairs.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Given a linked list, swap every two adjacent nodes and return its head.
3+
* <p>
4+
* For example,
5+
* Given 1->2->3->4, you should return the list as 2->1->4->3.
6+
* <p>
7+
* Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
8+
* <p>
9+
* Created by drfish on 16/04/2017.
10+
*/
11+
public class _024SwapNodesInPairs {
12+
public ListNode swapPairs(ListNode head) {
13+
if (head == null || head.next == null)
14+
return head;
15+
ListNode dummy = new ListNode(-1);
16+
dummy.next = head;
17+
for (ListNode prev = dummy, cur = prev.next, next = cur.next;
18+
next != null;
19+
prev = cur, cur = cur.next, next = cur != null ? cur.next : null) {
20+
prev.next = next;
21+
cur.next = next.next;
22+
next.next = cur;
23+
}
24+
return dummy.next;
25+
}
26+
}

java/_25ReverNodesInkGroup.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
3+
* <p>
4+
* k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
5+
* <p>
6+
* You may not alter the values in the nodes, only nodes itself may be changed.
7+
* <p>
8+
* Only constant memory is allowed.
9+
* <p>
10+
* For example,
11+
* Given this linked list: 1->2->3->4->5
12+
* <p>
13+
* For k = 2, you should return: 2->1->4->3->5
14+
* <p>
15+
* For k = 3, you should return: 3->2->1->4->5
16+
* <p>
17+
* Created by drfish on 16/04/2017.
18+
*/
19+
public class _25ReverNodesInkGroup {
20+
public ListNode reverseKGroup(ListNode head, int k) {
21+
if (head == null || k <= 1)
22+
return head;
23+
ListNode dummy = new ListNode(-1);
24+
dummy.next = head;
25+
for (ListNode prev = dummy, end = head; end != null; end = prev.next) {
26+
for (int i = 1; i < k && end != null; i++)
27+
end = end.next;
28+
if (end == null)
29+
break;
30+
prev = reverse(prev, prev.next, end);
31+
}
32+
return dummy.next;
33+
}
34+
35+
private ListNode reverse(ListNode prev, ListNode start, ListNode end) {
36+
ListNode endNext = end.next;
37+
for (ListNode p = start, cur = p.next, next = cur.next;
38+
cur != endNext;
39+
p = cur, cur = next, next = next != null ? next.next : null)
40+
cur.next = p;
41+
start.next = endNext;
42+
prev.next = end;
43+
return start;
44+
}
45+
}

0 commit comments

Comments
 (0)