File tree Expand file tree Collapse file tree 3 files changed +146
-0
lines changed Expand file tree Collapse file tree 3 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=21 lang=java
3
+ *
4
+ * [21] 合并两个有序链表
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Definition for singly-linked list.
10
+ * public class ListNode {
11
+ * int val;
12
+ * ListNode next;
13
+ * ListNode() {}
14
+ * ListNode(int val) { this.val = val; }
15
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16
+ * }
17
+ */
18
+ class Solution {
19
+ public ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
20
+ ListNode dummy = new ListNode ();
21
+ ListNode curr = dummy ;
22
+ while (list1 != null && list2 != null ) {
23
+ if (list1 .val < list2 .val ) {
24
+ curr .next = list1 ;
25
+ list1 = list1 .next ;
26
+ } else {
27
+ curr .next = list2 ;
28
+ list2 = list2 .next ;
29
+ }
30
+
31
+ curr = curr .next ;
32
+ }
33
+
34
+ if (list1 != null ) {
35
+ curr .next = list1 ;
36
+ }
37
+
38
+ if (list2 != null ) {
39
+ curr .next = list2 ;
40
+ }
41
+
42
+ return dummy .next ;
43
+ }
44
+ }
45
+ // @lc code=end
46
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=86 lang=java
3
+ *
4
+ * [86] 分隔链表
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Definition for singly-linked list.
10
+ * public class ListNode {
11
+ * int val;
12
+ * ListNode next;
13
+ * ListNode() {}
14
+ * ListNode(int val) { this.val = val; }
15
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16
+ * }
17
+ */
18
+ class Solution {
19
+ public ListNode partition (ListNode head , int x ) {
20
+ if (head == null ) return null ;
21
+ ListNode node1 = new ListNode ();
22
+ ListNode curr1 = node1 ;
23
+ ListNode node2 = new ListNode ();
24
+ ListNode curr2 = node2 ;
25
+
26
+ while (head != null ) {
27
+ if (head .val < x ) {
28
+ curr1 .next = head ;
29
+ curr1 = curr1 .next ;
30
+ } else {
31
+ curr2 .next = head ;
32
+ curr2 = curr2 .next ;
33
+ }
34
+
35
+ head = head .next ;
36
+ }
37
+
38
+ curr1 .next = node2 .next ;
39
+ curr2 .next = null ;
40
+ return node1 .next ;
41
+ }
42
+ }
43
+ // @lc code=end
44
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=92 lang=java
3
+ *
4
+ * [92] 反转链表 II
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Definition for singly-linked list.
10
+ * public class ListNode {
11
+ * int val;
12
+ * ListNode next;
13
+ * ListNode() {}
14
+ * ListNode(int val) { this.val = val; }
15
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16
+ * }
17
+ */
18
+ class Solution {
19
+ public ListNode reverseBetween (ListNode head , int left , int right ) {
20
+ if (head == null ) {
21
+ return head ;
22
+ }
23
+
24
+ ListNode dummy = new ListNode (0 );
25
+ dummy .next = head ;
26
+
27
+ ListNode pre = null ;
28
+ int i = 1 ;
29
+ while (i < left ) {
30
+ pre = head ;
31
+ head = head .next ;
32
+ i ++;
33
+ }
34
+
35
+ int j = i ;
36
+ ListNode next = null ;
37
+ ListNode mid = head ;
38
+ while (head != null && j <= right ) {
39
+ ListNode temp = head .next ;
40
+ head .next = next ;
41
+ next = head ;
42
+ head = temp ;
43
+ j ++;
44
+ }
45
+
46
+ if (pre != null ) {
47
+ pre .next = next ;
48
+ } else {
49
+ dummy .next = next ;
50
+ }
51
+ mid .next = head ;
52
+ return dummy .next ;
53
+ }
54
+ }
55
+ // @lc code=end
56
+
You can’t perform that action at this time.
0 commit comments