File tree 2 files changed +68
-0
lines changed
2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-07-04-13:37
6
+ * @Function Description :25.合并两个排序的链表
7
+ */
8
+ public class _25MergeTwoSortedLists {
9
+ /**
10
+ * Definition for singly-linked list.
11
+ * public class ListNode {
12
+ * int val;
13
+ * ListNode next;
14
+ * ListNode(int x) { val = x; }
15
+ * }
16
+ */
17
+ class Solution {
18
+ public ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
19
+ ListNode virtual = new ListNode (-1 ) , p = virtual ;
20
+ while ( l1 != null && l2 != null ){
21
+ if ( l1 .val <= l2 .val ) {
22
+ p .next = l1 ;
23
+ l1 = l1 .next ;
24
+ }else {
25
+ p .next = l2 ;
26
+ l2 = l2 .next ;
27
+ }
28
+ p = p .next ;
29
+ }
30
+
31
+ p .next = l1 != null ? l1 : l2 ;
32
+
33
+ return virtual .next ;
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-07-04-14:06
6
+ * @Function Description :52. 两个链表的第一个公共节点
7
+ */
8
+ public class _52TheFirstCommonNodeOfTwoLinkedLists {
9
+ /**
10
+ * Definition for singly-linked list.
11
+ * public class ListNode {
12
+ * int val;
13
+ * ListNode next;
14
+ * ListNode(int x) {
15
+ * val = x;
16
+ * next = null;
17
+ * }
18
+ * }
19
+ */
20
+ public class Solution {
21
+ public ListNode getIntersectionNode (ListNode headA , ListNode headB ) {
22
+ ListNode p1 = headA , p2 = headB ;
23
+ while ( p1 != p2 ){
24
+ // a链表走完走b,两个交叉走,这样就不用判断边界空的情况
25
+ p1 = p1 != null ? p1 .next : headB ;
26
+ p2 = p2 != null ? p2 .next : headA ;
27
+ }
28
+ // 如果没有相交的点,最终p1和p2都会走到null
29
+ return p1 ;
30
+ }
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments