Linked List Swap nodes java solution


  • 0
    V

    public class Solution {

    public ListNode swapPairs(ListNode head) {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        
        int buffer = 0;
        buffer = head.val;
        head.val = head.next.val;
        head.next.val = buffer;
        head.next.next = swapPairs(head.next.next);
        return head;
    }
    

    }


  • 0
    H

    I am afraid it is incorrect, since:
    You may not modify the values in the list, only nodes itself can be changed.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.