0%

两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode head = new ListNode(0);
ListNode p = head;
ListNode pre = p;
int temp;


while (l1.next != null && l2.next != null) {
temp = l1.val + l2.val;
if (p.val + temp < 10) {
p.val += temp;
p.next = new ListNode(0);
p = p.next; l1 = l1.next; l2 = l2.next;
} else {
p.val = (p.val + temp) % 10;
p.next = new ListNode(1);
p = p.next; l1 = l1.next; l2 = l2.next;
}
}

temp = l1.val + l2.val;
if (p.val + temp < 10) {
p.val += temp;
p.next = new ListNode(0);
pre = p;
p = p.next;
} else {
p.val = (p.val + temp) % 10;
p.next = new ListNode(1);
p = p.next;
}

while (l1.next != null) {
l1 = l1.next;
if (p.val + l1.val < 10) {
p.val += l1.val;
p.next = new ListNode(0);
pre = p;
p = p.next;
} else {
p.val = (p.val + l1.val) % 10;
p.next = new ListNode(1);
p = p.next;
}
}

while (l2.next != null) {
l2 = l2.next;
if (p.val + l2.val < 10) {
p.val += l2.val;
p.next = new ListNode(0);
pre = p;
p = p.next;
} else {
p.val = (p.val + l2.val) % 10;
p.next = new ListNode(1);
p = p.next;
}
}

if (p.val == 0) {
pre.next = null;
}

return head;
}
}

/**
执行用时 :3 ms, 在所有 Java 提交中击败了39.78%的用户
内存消耗 :41.1 MB, 在所有 Java 提交中击败了92.22%的用户
*/

Reference
https://leetcode-cn.com/problems/add-two-numbers

环形链表

给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> container = new HashSet<>();

while (head != null) {
if (container.contains(head)) {
return true;
}
container.add(head);
head = head.next;
}

return false;
}
}

img
Put each ListNode pointer into the Set container.
Because every pointer is unique, we just to judge if there’s a pointer’s address we scan has already contained in Set.

Reference
https://leetcode-cn.com/problems/linked-list-cycle

Implements & Extends 的不同

在类的声明中,通过关键字extends来创建一个类的子类;一个类通过关键字implements声明自己使用一个或者多个接口。

  • extends 是继承某个类, 继承之后可以使用父类的方法, 也可以重写父类的方法;

  • implements 是实现多个接口, 接口的方法一般为空的, 必须重写才能使用;

  • extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承;

  • Java中不支持多重继承,但是可以用接口来实现,这样就要用到implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了 比如 :class A extends B implements C,D,E

接口实现的注意点:

  • 实现一个接口就是要实现该接口的所有的方法(抽象类除外);

  • 接口中的方法都是抽象的;

  • 多个无关的类可以实现同一个接口,一个类可以实现多个无关的接口。

Implements与Extends的不同

extends, 可以实现父类,也可以调用父类初始化 this.parent()。而且会覆盖父类定义的变量或者函数。这样的好处是:架构师定义好接口,让工程师实现就可以了。整个项目开发效率和开发成本大大降低。

implements,实现父类,子类不可以覆盖父类的方法或者变量。即使子类定义与父类相同的变量或者函数,也会被父类取代掉。

这两种实现的具体使用,是要看项目的实际情况,需要实现,不可以修改implements,只定义接口需要具体实现,或者可以被修改扩展性好,用extends。

Reference
https://blog.csdn.net/tolcf/article/details/46135645

删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2
示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp = head;

if (head == null) {
return null;
}

while (temp.next != null) {
if (temp.val == temp.next.val) {
temp.next = temp.next.next;
continue;
}
temp = temp.next;
}

return head;
}
}

/**
执行用时 :1 ms, 在所有 Java 提交中击败了94.59%的用户
内存消耗 :39.3 MB, 在所有 Java 提交中击败了5.00%的用户
*/

Base Case: head==null
Because this is a sorted linked list, we just need to figure out whether the next node’s values equals current node’s value.

Reference
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list

合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

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
ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null | l2 == null) {
if (l1 == null & l2 == null) {
return null;
}
if (l1 != null) {
return l1;
}
if (l2 != null) {
return l2;
}
}
ListNode res = new ListNode(0);
ListNode temp = res;
while (l1 != null & l2 != null) {
if (l1.val <= l2.val) {
temp.val = l1.val;
temp.next = new ListNode(0);
temp = temp.next;
l1 = l1.next;
} else {
temp.val = l2.val;
temp.next = new ListNode(0);
temp = temp.next;
l2 = l2.next;
}
}

if (l1 != null) {
temp.val = l1.val;
while (l1.next != null) {
temp.next = new ListNode(l1.next.val);
temp = temp.next;
l1 = l1.next;
}
} else {
temp.val = l2.val;
while (l2.next != null) {
temp.next = new ListNode(l2.next.val);
temp = temp.next;
l2 = l2.next;
}
}

return res;

/*
执行用时 :1 ms, 在所有 Java 提交中击败了83.58%的用户
内存消耗 :38.5 MB, 在所有 Java 提交中击败了51.34%的用户
*/
}

The code is too complicated. I’m going on to simplify it.

Here is an updated code after I read the official document.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res = new ListNode(0);// Head Pointer
ListNode temp = res;
while (l1 != null & l2 != null) {
if (l1.val <= l2.val) {
temp.next = l1;
l1 = l1.next;
} else {
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
temp.next = (l1 == null) ? l2 : l1;
return res.next;

/* 执行用时 :1 ms, 在所有 Java 提交中击败了83.58%的用户
内存消耗 :38.2 MB, 在所有 Java 提交中击败了55.50%的用户
*/
}

This is a nice solution to have a head pointer, which means we haven’t to modified current temp.val and register a new object on the temp.next. Also we needn’t to care whether l1 or l2 is null

Reference
https://leetcode-cn.com/problems/merge-two-sorted-lists