关于单链表、双向列表的一些算法

首先给出数据定义的结构

单链表

/**
 * 链表
 */

public class LinkTable {
  public LinkTable next;
  public String name;

  public void setName(String name) {
    this.name = name;
  }

  public LinkTable(String name) {
    this.name = name;
  }
}
1.单链表的逆序反转
package zong.xiao.mi.demosource.java;

/**
 * Created by mi on 2017/3/9.
 */

public class 单链表的倒序 {

  public static void main(String[] a) {
    LinkTable linkTable = LinkTableReverse2(getLinkTable());
    print(linkTable);

  }

  public static LinkTable getLinkTable() {
    LinkTable A = new LinkTable("A");
    LinkTable B = new LinkTable("B");
    LinkTable C = new LinkTable("C");
    LinkTable D = new LinkTable("D");
    LinkTable E = new LinkTable("E");
    A.next = B;
    B.next = C;
    C.next = D;
    D.next = E;
    return A;
  }

  /**单链表逆序(反转)
   * 非递归
   * 1.把root的next拿出来
      2.把root.next赋值 
      3.把头部newRoot存起来
      4.更换root为为head
   * @param root
   */
  public static void LinkTableReverse(LinkTable root) {
    print(root);
    LinkTable newRoot = null;
    while (root != null) {
      LinkTable next = root.next;
      root.next = newRoot;
      newRoot = root;
      root = next;
    }
    print(newRoot);
  }

  /**
   * 递归 递归的思想就是找到最后一个节点 返回到上一层,
   * 让root.next.next=root 回溯 root.nex=null;
   * 
   * @param root
   * @return
   */
  public static LinkTable LinkTableReverse2(LinkTable root) {
    LinkTable newRoot = null;

    if (root == null || root.next == null) {
      return root;
    }
    newRoot = LinkTableReverse2(root.next);
    root.next.next = root;
    root.next = null;
    return newRoot;

  }

单链表相邻节点反转 A-B-C-D 输出 B-A-D-C

/**
   * 链表相邻两元素反转
   * 比如A-B-C-D-E输出B-A-D-C-E
   * 
   * @param root
   * @return
   */
  public static LinkTable borderReverse(LinkTable root) {

    if (root == null || root.next == null) return root;
    LinkTable curr = root;
    LinkTable next;
    LinkTable pre = null;
    while (curr != null && curr.next != null) {

      if (pre != null) {
        pre.next = curr.next;
      } else {
        root = curr.next;
      }
      pre = curr;
      next = curr.next.next;
      curr.next.next = curr;
      curr.next = next;
      curr = curr.next;
    }
    return root;

  }
  public static void print(LinkTable root) {
    while (root != null) {
      System.out.print(root.name);
      System.out.print("--");
      root = root.next;
    }
    System.out.println();
  }
}

双向链表

public class Node {

  public Node pre;
  public Node next;
  public int data;

  public Node(Node pre, Node next, int data) {
    this.pre = pre;
    this.next = next;
    this.data = data;
  }

  public static void printNode(Node node) {
    Node n = node;
    while (n != null) {
      System.out.println(n.data);
      n = n.next;
    }

  }
}

双向链表的反转

 public Node reverse(Node node) {
    Node n = node, pre = null;
    while (n != null) {
      Node next = n.next;
      n.next = pre;
      n.pre = next;
      pre = n;
      n = next;
    }
    return pre;
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 10,653评论 0 12
  • 高级钳工应知鉴定题库(858题) ***单选题*** 1. 000003难易程度:较难知识范围:相关4 01答案:...
    开源时代阅读 11,315评论 1 9
  • 故事里的事说是也是不是是也不是 我听过别人讲的故事 看过别人写的故事 也向别人分享过彼此...
    4ever1阅读 1,549评论 0 0
  • 看少林足球 励志题材 偏偏心酸到要流泪 我喜欢你啊 我也喜欢你 那我们之间是爱情吗 你在开玩笑吧 怎么可能是爱情 ...
    曜丛阅读 2,806评论 1 1
  • 演员的诞生因为微博营销,大量水军吹章子怡和不爽郑爽的段子,甚至还上了热搜榜,这不知道是真人刷上去的还是买的热搜,但...
    小兑兑ss阅读 3,273评论 0 1

友情链接更多精彩内容