简体   繁体   中英

How do I use recursion to swap nodes in pairs in a linked list?

I am trying to implement code to swap two adjacent pairs in a linked list, and am having some trouble understanding where my mistake is.

This is for a leetcode problem implemented with the Java programming language, I first tried an iterative solution where I allocated a first initial node and a third initial node, then iterate all the nodes switching every 2. My second attempt is at a recursive solution with a base case checking if there is 0 or 1 node. Then I swapped the first 2 nodes then recursed through the rest of the linked list then join the linked list.

/**
 * 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 || (head.next == null)){return head;}
    //first we swap the first node and the second node
    ListNode first = head;
    ListNode third = head.next.next;
    first.next.next = first;
    first.next = third;



    //then we recurse on part of the linked list

    ListNode recursedList = swapPairs(head.next.next);

    //we join these two linked lists together
    first.next.next = recursedList;


    //and finally we return the head

    return head;
}
}

For a sample input
[1,2,3,4] the solution is [2,1,4,3] but my solution yields [1,3,4]. Where in my code is my logic flawed?

Believe it's just a simple mistake.

public ListNode swapPairs(ListNode head) {
    if(head == null || head.next == null) { return head; }

    # swapping the first and second
    ListNode second = new ListNode(head.val);
    ListNode first = new ListNode(head.next.val);
    first.next = second;

    # recursively swap the next 2 items in the linked list till the end of list
    ListNode recursedList = swapPairs(head.next.next);

    first.next.next = recursedList;

    return first;
}

This should work for both cases of even and odd number of nodes in the linked list.

Main mistake is that you were swapping the first node and the third node, not the first and the second (not adjacent pairs). Also, assignments such as ListNode first = head; only makes shallow copies. This means that if you were to try the following...

printLinkedList(head); # function to print out the entire linked list
ListNode first = head;
first.val = 100;
first.next = head.next.next.next;
printLinkedList(head);

... you will find that changing first also changed head , and the print results you get will be as follows:

1 -> 2 -> 3 -> 4 -> null
100 -> 4 -> null

One more approach

public ListNode swapPairs(ListNode head) {
    if ((head == null)||(head.next == null))
        return head;
    ListNode n = head.next;
    head.next = swapPairs(head.next.next);
    n.next = head;
    return n;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM