zoukankan      html  css  js  c++  java
  • java 链表转置

    public class Test {

    public static void main(String[] args) {
    ListNode listNode = new ListNode(1);
    int n = 2;
    ListNode head = listNode;
    while (n < 10) {
    head.next = new ListNode(n);
    head = head.next;
    n++;
    }
    printfListNode(listNode);
    reverseBetween(listNode, 2, 4);
    printfListNode(listNode);
    }

    public static class ListNode {
    int value;
    ListNode next;

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

    public static void printfListNode(ListNode listNode) {
    ListNode head = listNode;
    while (null != head.next) {
    System.out.println(head.value);
    System.out.println(",");
    head = head.next;
    }
    }

    public static ListNode reverseBetween(ListNode head, int m, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy;
    for (int i = 0; i < m; i++) {
    pre = pre.next;
    }
    head = pre.next;
    System.out.println(dummy);
    for (int i = m; i < n; i++) {
    ListNode nex = head.next;
    head.next = nex.next;
    nex.next = pre.next;
    pre.next = nex;
    }
    return dummy.next;
    }
    }

  • 相关阅读:
    ANSI C 与 C99的不同
    字符串中含有空格的注意事项
    巧用printf函数
    求数列的和
    数值统计
    平方和与立方和
    求奇数的乘积
    第几天?
    细节之重
    用%*c滤掉回车,ASCII码排序
  • 原文地址:https://www.cnblogs.com/mohanchen/p/12871430.html
Copyright © 2011-2022 走看看