简体   繁体   中英

Reversing K nodes in a Linked List

I have implemented a java code that reverses every k Node in the linked list and here is the code:

public class ReverseALinkedList {

    public static void main(String[] args) {
        int k = 2;
        Node a = new Node(1);
        Node b = new Node(2);
        Node c = new Node(3);
        Node d = new Node(4);
        Node e = new Node(5);
        Node f = new Node(6);
        Node g = new Node(7);
        Node h = new Node(8);
        a.next = b;
        b.next = c;
        c.next = d;
        d.next = e;
        e.next = f;
        f.next = g;
        g.next = h;
        a.printLinkedList();
        Node head = reverseKLinkedList(a, k);
        head.printLinkedList();
    }

    private static Node reverseKLinkedList(Node first, int k) {
        Node current;
        Node temp;
        int count = 0;

        current = null;

        while (first != null && count < k) {
            temp = first.next;
            first.next = current;
            current = first;
            first = temp;
            ++count;
        }

        if (first != null) {
            first.next = reverseKLinkedList(first, k);
        }
        return current;
    }

    static class Node {
        public Node next;
        public int value;

        public Node(int value) {
            this.value = value;
        }

        public void printLinkedList() {
            Node head = this;
            while (head.next != null) {
                System.out.print(head.value + "->");
                head = head.next;
            }
            System.out.print(head.value + "->null");
            System.out.println();
        }

    }
}

When I execute the code with the following linked list:

1->2->3->4->5->6->null and k set to 2, I get an output as follows:

2->1->null

The rest of the nodes gets reversed (ie, 4->3, 6->5), but they are not returned during the recursive call.

Could anybody please let me know how to fix this?

Another way of doing it is to ITERATE every K nodes and store it in a S stack like manner. Wherein every S is stored in N newList in every K iteration.

private static Node reverseKLinkedList(Node first, int k){
  Node newList, temp, current, walk, node;
  int count;

  node = first;
  newList = null;
  while(node != null) {
    count = 0;

    // stack the nodes to current until node is null or count is less than k
    current = null;
    while(node != null && count < k) {
      temp = current;
      current = new Node(node.value);
      current.next = temp;
      node = node.next;
      count++;
    }

    if(newList == null) // if newList is empty then assign the current node
      newList = current;
    else { 
      // else traverse against the newList until it reaches 
      // the last node and then append the current not
      walk = newList;
      while(walk.next != null) walk = walk.next;
      walk.next = current;
    }
  }

  return newList;

}

As you are reversing the first one you are losing the reference to the next Node and therefore you lose the rest of the elements. To implement this you should use recursion in order to store nodes or a stack(which is basically the same as recursion).

Proceed like this:

Store all the Nodes from the list in a stack.
For each node poped, the next elem is the top of the stack.
Repeat until no more nodes left.

Here is the recursive Java implementation

public static <T> LinearNode<T> reverseAlternateKNodes(LinearNode<T> node, int k) {
    int count = 0;
    LinearNode<T> curr=node, prev = null, next = null;
    //reverse k nodes
    while (curr != null && count < k) {
        next = curr.next();
        curr.next(prev);
        prev = curr;
        curr = next;
        count ++;
    }
    // head should point to start of next k node
    node.next(curr);
    // skip nex knodes
    count = 0;
    while (curr != null && count < k- 1) {
        curr = curr.next();
        count ++;
    }
    // do it recursively
    if (curr != null) {         
        curr.next(reverseAlternateKNodes(curr.next(), k));
    }

    return prev;
}

Here is the unit tests

@Test
public void reverseAlternateKNodes() {
    LinearNode<Integer> head = buildLinkedList(1,2,3,4,5,6,7,8,9,10);
    head = LinkedListUtil.reverseAlternateKNodes(head, 3);
    assertLinkedList(head, 3,2,1,4,5,6,9,8,7,10);
}
 public ListNode reverseKGroup(ListNode head, int k) {
    ListNode curr = head;
    int count = 0;
    while(curr!=null && count!=k){
        count++;
        curr = curr.next;
    }
    if(count==k){
        curr = reverseKGroup(curr,k);
        while(count-->0){
            ListNode temp = head.next;
            head.next = curr;
            curr = head;
            head = temp;
        }
        head = curr;
    }
    return head;
}

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