zoukankan      html  css  js  c++  java
  • 编程题之--链表反置

      对链表进行反置。例如:

      [1] -> [2] -> [3] -> [null]

    变成:

      [3] -> [2] -> [1] -> [null]

    我的思路如下:

      顺着链表循环一次,当走到某个节点上时,把当前节点的next设为上一个节点,但是,为了不丢失下一个节点的引用,需要在设置next为上一个节点之前,用一个临时变量保存下个节点的引用,具体过程如下,链表长度为3:其中(p)、(c)、(n)下标分别表示previous,current,next,即上一个,当前和下一个节点。

    参考代码:

    1) LinkNode.java

     1 public class LinkNode<T> {
     2 
     3     private T data;
     4     private LinkNode<T> next = null;
     5 
     6     public LinkNode(T data) {
     7         this.data = data;
     8         this.next = null;
     9     }
    10 
    11     public T getData() {
    12         return data;
    13     }
    14 
    15     public void setData(T data) {
    16         this.data = data;
    17     }
    18 
    19     public boolean hasNext() {
    20         return this.next != null;
    21     }
    22 
    23     public LinkNode<T> getNext() {
    24         return next;
    25     }
    26 
    27     public void setNext(LinkNode<T> next) {
    28         this.next = next;
    29     }
    30 
    31     public void display() {
    32         System.out.printf(" {%s} ", data.toString());
    33     }
    34     
    35 }

    2) LinkList.java,链表反置的函数为下面代码中的reverse()函数。

     1 public class LinkList<T> {
     2 
     3     private LinkNode<T> first;
     4 
     5     public LinkList() {
     6         this.first = null;
     7     }
     8 
     9     public boolean isEmpty() {
    10         return this.first == null;
    11     }
    12 
    13     public LinkNode<T> getFirst() {
    14         return first;
    15     }
    16 
    17     public void setFirst(LinkNode<T> first) {
    18         this.first = first;
    19     }
    20 
    21     public void insertFirst(T newEle) {
    22         LinkNode<T> tLinkNode = new LinkNode<>(newEle);
    23         tLinkNode.setNext(first);
    24         first = tLinkNode;
    25     }
    26 
    27     public void displayList() {
    28         System.out.println("List(first-->last): ");
    29         LinkNode<T> current = first;
    30         while (current != null) {
    31             current.display();
    32             current = current.getNext();
    33         }
    34         System.out.println(" ");
    35     }
    36 
    37     public void reverse() {
    38         if (this.first == null) {
    39             return;
    40         }
    41         LinkNode<T> previous = null;
    42         LinkNode<T> current = this.first;
    43         LinkNode<T> next = current.getNext();
    44 
    45         while (current != null) {
    46             current.setNext(previous);
    47 
    48             previous = current;
    49             current = next;
    50             next = current != null ? current.getNext() : null;
    51         }
    52         this.first = previous;
    53     }
    54 
    55     public static void main(String[] args) {
    56         LinkList<Integer> linkList = new LinkList<>();
    57         linkList.insertFirst(5);
    58         linkList.insertFirst(4);
    59         linkList.insertFirst(3);
    60         linkList.insertFirst(2);
    61         linkList.insertFirst(1);
    62         linkList.displayList();
    63 
    64         linkList.reverse();
    65         linkList.displayList();
    66 
    67     }
    68 }

    下面介绍一下其他思路:

      [1] -> [2] -> [3] -> [null]

    变成:

      [3] -> [2] -> [1] -> [null]

      思路1:创建一个新的链表,把原来链表上节点逐个拆下来,然后作为头结点插入到新链表中,新链表即为反转后的链表

     1     public LinkList<T> reverseNew() {
     2         LinkList<T> reverse = new LinkList<>();
     3         LinkNode<T> current = this.first;
     4         while (current != null) {
     5             this.deleteFirst();
     6             reverse.insertFirst(current);
     7             current = this.first;
     8         }
     9         return reverse;
    10     }
  • 相关阅读:
    Class 、NSObject、id
    xcode8.2 打包问题
    JS WEB 交互问题
    C语言 关于内存动态分配问题
    Objective-C( Category 分类,非正式协议,分类延展)
    NSComparisonResul、NSNotFound、NSEnumerationOptions......的用处
    Objective-C( Foundation框架 一 常见的结构体)
    Objective-C( Foundation框架 一 数组(NSMutableArray))
    Objective-C( Foundation框架 一 数组(NSArray))
    Objective-C( Foundation框架 一 字符串)
  • 原文地址:https://www.cnblogs.com/tlz888/p/7173118.html
Copyright © 2011-2022 走看看