-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb8b064
commit dbfb90d
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
0024_Swap Nodes in Pairs/SwapNodesinPaires_Iteration3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |