Swap Two Nodes in Linked List

LintCode-511.Swap Two Nodes in Linked List

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing.

Notice

You should swap the two nodes with values v1 and v2. Do not directly swap the values of the two nodes.

Example

Given 1->2->3->4->null and v1 = 2, v2 = 4.

Return 1->4->3->2->null.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param head a ListNode
     * @oaram v1 an integer
     * @param v2 an integer
     * @return a new head of singly-linked list
     */
    public ListNode swapNodes(ListNode head, int v1, int v2) {
        if (head == null) {
            return null;
        }
        
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        
        ListNode node = dummy;
        ListNode node1Prev = null, node2Prev = null;
        
        while (node != null && node.next != null) {
            if (node.next.val == v1) {
                node1Prev = node;
            } else if (node.next.val == v2) {
                node2Prev = node;
            }
            
            node = node.next;
        }
        
        if (node1Prev == null || node2Prev == null) {
            return head;
        }
        
        if (node2Prev.next == node1Prev) {
            ListNode temp = node1Prev;
            node1Prev = node2Prev;
            node2Prev = temp;
        }
        
        ListNode node1 = node1Prev.next;
        ListNode node2 = node2Prev.next;
        ListNode node2Next = node2.next;
        
        if (node1Prev.next == node2Prev) {
            node1Prev.next = node2;
            node2.next = node1;
            node1.next = node2Next;
        } else {
            node1Prev.next = node2;
            node2.next = node1.next;
            
            node2Prev.next = node1;
            node1.next = node2Next;
        }
        
        return dummy.next;
    }
}

Hope this helps,
Michael

DigitalOcean Referral Badge