c# iterative solution


  • 0
    N
    public class Solution {
        public ListNode SwapPairs(ListNode head) {
            var fakehead = new ListNode(0);
            fakehead.next = head;
            var pre = fakehead;
            while(pre.next!=null&& pre.next.next!=null){
                var node = pre.next;
                var temp =node.next;
                node.next = node.next.next;
                temp.next = node;
                pre.next = temp;
                pre = node;
            }
            
            return fakehead.next;
        }
    }
    

Log in to reply
 

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