Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public ListNode swapPairs(ListNode head) {
- if (head == null){
- return null;
- }
- if (head.next == null){
- return head;
- }
- ListNode returnNode = head.next;
- ListNode nextNext = returnNode.next;
- returnNode.next = head;
- recSwap(nextNext, head);
- return returnNode;
- }
- public void recSwap(ListNode curr, ListNode temp){
- if (curr == null){
- temp.next = curr;
- } else if (curr.next == null){
- temp.next = curr;
- } else {
- ListNode next = curr.next;
- ListNode newCurr = next.next;
- next.next = curr;
- temp.next = next;
- recSwap(newCurr, curr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement