Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on…
单链表的逆置是一个非常经典的问题,这里利用两个思想进行解决. 首先,我们需要看下原理图,其实两个思想都是一样的,都是使后一个的节点的 next 指针指向前一个节点,依次递推,直到第二个节点指向第一个节点,第一个节点的 next 指针指向 NULL. 第一种方法: 在链表往前走的过程中,记录前一个节点,当前节点和后一个节点,并使当前节点的 next 指针指向前一个节点,直到最后一个节点指向倒数第二个节点 算法实现如下: void reve…
class LNode { public LNode next; public int data; } /*逆置链表*/ class Nizhi { private static LNode head = new LNode();; private static LNode node; private static LNode tail; private static int index; private static LNode newhead = new LNode(); public st…
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must outp…