File tree 6 files changed +113
-2
lines changed
intersection-of-two-linked-lists
intersection-of-two-linked-lists 6 files changed +113
-2
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) {
7
+ * val = x;
8
+ * next = null;
9
+ * }
10
+ * }
11
+ */
12
+ public class Solution {
13
+
14
+ int len (ListNode head ){
15
+
16
+ int len = 0 ;
17
+ ListNode iter = head ;
18
+
19
+ while (iter != null ){
20
+
21
+ len ++;
22
+ iter = iter .next ;
23
+ }
24
+
25
+ return len ;
26
+ }
27
+
28
+ ListNode trim (ListNode head , int count ){
29
+
30
+ ListNode iter = head ;
31
+
32
+ while (iter != null && count > 0 ){
33
+
34
+ count --;
35
+ iter = iter .next ;
36
+ }
37
+
38
+ return iter ;
39
+ }
40
+
41
+
42
+ public ListNode getIntersectionNode (ListNode headA , ListNode headB ) {
43
+
44
+
45
+ int lenA = len (headA );
46
+ int lenB = len (headB );
47
+ int min = Math .min (lenA , lenB );
48
+
49
+ ListNode iterA = trim (headA , lenA - min );
50
+ ListNode iterB = trim (headB , lenB - min );
51
+
52
+ ListNode intersection = null ;
53
+
54
+
55
+ while (iterA != null && iterB != null ){
56
+
57
+ if (iterA .val != iterB .val ){
58
+ intersection = null ;
59
+ } else if (intersection == null ){
60
+ intersection = iterA ;
61
+ }
62
+
63
+ iterA = iterA .next ;
64
+ iterB = iterB .next ;
65
+ }
66
+
67
+
68
+ if (iterA != null || iterB != null ){
69
+ return null ;
70
+ }
71
+
72
+ return intersection ;
73
+ }
74
+ }
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public boolean isOneEditDistance (String s , String t ) {
3
+
4
+ char [] S = s .toCharArray ();
5
+ char [] T = t .toCharArray ();
6
+
7
+ // make sure s is always to shorter one
8
+ if (S .length > T .length ) return isOneEditDistance (t , s );
9
+
10
+ if (T .length - S .length > 1 ) return false ;
11
+
12
+ int diff = 0 ;
13
+
14
+ int i = 0 ;
15
+ int j = 0 ;
16
+
17
+ for (/* void */ ; i < S .length ; i ++, j ++){
18
+ if (S [i ] != T [j ]){
19
+
20
+ diff ++;
21
+
22
+ if (diff > 1 ) return false ;
23
+
24
+ // len(s) + 1 = len(t)
25
+ if (S .length != T .length && S [i ] == T [j + 1 ]){
26
+ j ++; // delete one from T
27
+ }
28
+ }
29
+ }
30
+
31
+ if (diff == 0 ){
32
+ return j + 1 == T .length ;
33
+ }
34
+
35
+ return j == T .length ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change 1
1
---
2
2
layout : solution
3
3
title : Intersection of Two Linked Lists
4
- date : 2014-12-02 13:08:34+08:00
4
+ date : 2014-12-02 13:09:51 +0800
5
5
---
6
6
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
7
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
Original file line number Diff line number Diff line change 1
1
---
2
2
layout : solution
3
3
title : One Edit Distance
4
- date : 2014-12-03 00:46:25+08:00
4
+ date : 2014-12-03 00:47:41 +0800
5
5
---
6
6
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
7
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
You can’t perform that action at this time.
0 commit comments