File tree Expand file tree Collapse file tree 7 files changed +121
-33
lines changed Expand file tree Collapse file tree 7 files changed +121
-33
lines changed Original file line number Diff line number Diff line change @@ -47,4 +47,6 @@ class Solution {
47
47
}
48
48
}
49
49
if (rigth >= 0 ) {
50
- ```
50
+ ```
51
+
52
+ ! [](https: // ws1.sinaimg.cn/large/006tKfTcgy1g0vm0gawoyj30wx0u0wj1.jpg)
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package leetcode ._24_ ;
2
+
3
+ import leetcode .common .ListNode ;
4
+
5
+ /**
6
+ * Created by zhangbo54 on 2019-03-04.
7
+ */
8
+ public class Main {
9
+ public static void main (String [] args ) {
10
+ Solution solution = new Solution ();
11
+ ListNode node = new ListNode (1 )
12
+ .setNext (new ListNode (2 )
13
+ .setNext (new ListNode (3 )
14
+ .setNext (new ListNode (4 ))));
15
+ System .out .println (node );
16
+ System .out .println (solution .swapPairs (node ));
17
+
18
+ node = new ListNode (1 )
19
+ .setNext (new ListNode (2 )
20
+ .setNext (new ListNode (3 )));
21
+ System .out .println (node );
22
+ System .out .println (solution .swapPairs (node ));
23
+ }
24
+ }
25
+
Original file line number Diff line number Diff line change
1
+ package leetcode ._24_ ;
2
+
3
+ import leetcode .common .ListNode ;
4
+
5
+ /**
6
+ * Definition for singly-linked list.
7
+ * public class ListNode {
8
+ * int val;
9
+ * ListNode next;
10
+ * ListNode(int x) { val = x; }
11
+ * }
12
+ */
13
+ class Solution {
14
+ public ListNode swapPairs (ListNode head ) {
15
+ if (head == null || head .next == null ) {
16
+ return head ;
17
+ }
18
+ ListNode temp = head .next ;
19
+ head .next = temp .next ;
20
+ temp .next = head ;
21
+ head = temp ;
22
+
23
+ ListNode frontNode = head .next .next ;
24
+ ListNode backNode = head .next ;
25
+ while (frontNode != null && frontNode .next != null ) {
26
+ temp = frontNode .next ;
27
+ backNode .next = temp ;
28
+ frontNode .next = temp .next ;
29
+ temp .next = frontNode ;
30
+ backNode = backNode .next ;
31
+
32
+ frontNode = frontNode .next ;
33
+ backNode = backNode .next ;
34
+ }
35
+ return head ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ ### [ 24\. Swap Nodes in Pairs] ( https://leetcode.com/problems/swap-nodes-in-pairs/ )
2
+
3
+ Difficulty: ** Medium**
4
+
5
+
6
+ Given a linked list, swap every two adjacent nodes and return its head.
7
+
8
+ You may ** not** modify the values in the list's nodes, only nodes itself may be changed.
9
+
10
+ ** Example:**
11
+
12
+ ```
13
+ Given 1->2->3->4, you should return the list as 2->1->4->3.
14
+ ```
15
+
16
+
17
+ #### Solution
18
+
19
+ Language: ** Java**
20
+
21
+ ``` java
22
+ /**
23
+ * Definition for singly-linked list.
24
+ * public class ListNode {
25
+ * int val;
26
+ * ListNode next;
27
+ * ListNode(int x) { val = x; }
28
+ * }
29
+ */
30
+ class Solution {
31
+ public ListNode swapPairs (ListNode head ) {
32
+ if (head == null || head. next == null ) {
33
+ return head;
34
+ }
35
+ ListNode temp = head. next;
36
+ head. next = temp. next;
37
+ temp. next = head;
38
+ head = temp;
39
+
40
+ ListNode frontNode = head. next. next;
41
+ ListNode backNode = head. next;
42
+ while (frontNode != null && frontNode. next != null ) {
43
+ temp = frontNode. next;
44
+ backNode. next = temp;
45
+ frontNode. next = temp. next;
46
+ temp. next = frontNode;
47
+ backNode = backNode. next;
48
+
49
+ frontNode = frontNode. next;
50
+ backNode = backNode. next;
51
+ }
52
+ return head;
53
+ }
54
+ }
55
+ ```
56
+ ![ ] ( https://ws1.sinaimg.cn/large/006tKfTcgy1g0vlzv7nsqj310m0nstbv.jpg )
You can’t perform that action at this time.
0 commit comments