Skip to content

Commit d8d20d5

Browse files
committed
24. Swap Nodes in Pairs
1 parent f66916f commit d8d20d5

File tree

7 files changed

+121
-33
lines changed

7 files changed

+121
-33
lines changed

src/leetcode/_22_/solution.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ class Solution {
4747
  }
4848
}
4949
       if (rigth >= 0) {
50-
```
50+
```
51+
52+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g0vm0gawoyj30wx0u0wj1.jpg)

src/leetcode/_23_/Main.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/leetcode/_23_/Solution.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/leetcode/_23_/solution.md

Whitespace-only changes.

src/leetcode/_24_/Main.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+

src/leetcode/_24_/Solution.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

src/leetcode/_24_/solution.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy