Skip to content

Commit

Permalink
Leetcode 24 Swap Nodes in Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajionline committed Nov 10, 2023
1 parent cb8b064 commit dbfb90d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 0024_Swap Nodes in Pairs/SwapNodesinPaires_Iteration3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode preHead = new ListNode(-1);
preHead.next = head;
ListNode p = preHead;
while(p != null && p.next != null && p.next.next != null) {
ListNode next = p.next;
p.next = next.next;
next.next = p.next.next;
p.next.next = next;
p = next;
}
return preHead.next;
}
}

0 comments on commit dbfb90d

Please sign in to comment.