反转链接列表中的K节点

时间:2014-03-22 21:00:19

标签: java data-structures linked-list

我已经实现了一个java代码,它反转链表中的每个k节点,这里是代码:

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();
        }

    }
}

当我使用以下链接列表执行代码时:

1-> 2-> 3-> 4-> 5-> 6-> null且k设为2,得到如下输出:

2→1→空

其余节点被反转(即,4> 3,6-> 5),但在递归调用期间不返回它们。

有人可以告诉我如何解决这个问题吗?

4 个答案:

答案 0 :(得分:1)

另一种方法是迭代每个K个节点,并以S堆栈方式存储它。 其中S每个N在每个K次迭代中都存储在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; 个新列表中。

{{1}}

}

答案 1 :(得分:0)

当您正在反转第一个节点时,您将丢失对下一个节点的引用,因此您将丢失其余元素。 要实现这一点,你应该使用递归来存储节点或堆栈(这与递归基本相同)。

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.

答案 2 :(得分:0)

这是递归Java实现

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;
}

这是单元测试

@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);
}

答案 3 :(得分:0)

 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;
}