Skip to content

Commit 4349aa5

Browse files
authored
Create 1.SwapAdjacentNodesOfLinkedList.java
swapping nodes not the data.
1 parent 24a4852 commit 4349aa5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1.SwapAdjacentNodesOfLinkedList.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
13+
14+
public ListNode swapPairs(ListNode node) {
15+
16+
if (node == null || node.next == null) {
17+
return node;
18+
}
19+
20+
21+
ListNode remaining = node.next.next;
22+
ListNode newhead = node.next;
23+
node.next.next = node;
24+
node.next = swapPairs(remaining);
25+
return newhead;
26+
27+
28+
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)