my java simple solution


  • 0
    J

    public class Solution {
    public ListNode swapPairs(ListNode head) {
    ListNode tmp = new ListNode(0);
    tmp.next = head;
    while(tmp.next != null && tmp.next.next !=null){
    int a = 0;
    a = tmp.next.val;
    tmp.next.val = tmp.next.next.val;
    tmp.next.next.val = a;
    tmp = tmp.next.next;
    }
    return head;
    }
    }


  • 0
    P

    Hey, unfortunately this code doesn't work, if I understand your code properly. You seem to be switching the values of the nodes, but the problem calls for no value switches, only pointer switches.


Log in to reply
 

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