这个二级单链表只包括一些head public class Link { public Link Next; public int Data; public Link(Link next int data) { thisNext = next; thisData = data; } } public class CascadeLink { public Link Next; public CascadeLink NextHead; public CascadeLink(CascadeLink nextHead Link next) { thisNext = next; thisNextHead = nextHead; } } 下面做一个二级单链表GenerateLink和GenerateLink方法在前面都已经介绍过了 public static CascadeLink GenerateCascadeLink() { Link head = GenerateLink(); Link head = GenerateLink(); Link head = GenerateLink(); CascadeLink element = new CascadeLink(null head); CascadeLink element = new CascadeLink(element head); CascadeLink element = new CascadeLink(element head); CascadeLink head = new CascadeLink(element null); return head; } 就是说这些单链表的表头headheadheadhead……它们组成了一个二级单链表headnull –> head –> head –> head –> head –> 我们的算法思想是 进行两次遍历在外层用curr遍历二级单链表head在内层用curr遍历每个单链表 public static Link GenerateNewLink(CascadeLink head) { CascadeLink curr = headNextHead; Link newHead = currNext; Link curr = newHead; while (curr != null) { currNext = currNextNext; while (currNext != null) { curr = currNext; } curr = currNextHead; } return newHead; } 其中currNext = currNextNext; 这句话是关键它负责把上一个单链表的表尾和下一个单链表的非空表头连接起来 |