java反转链表 大神给我添加个注释好么,我自己研究

Python016

java反转链表 大神给我添加个注释好么,我自己研究,第1张

public class Linktest {

//反转方法 ,传入一个链表

    public static LinkNode reversal(LinkNode list){

//pre用来存放前一个链表节点

        LinkNode pre =list

//取出下一个链表节点 ,cru 用来存放当前链表节点        

        LinkNode cru = list.getNext()

//next用来存放下一个链表节点        

        LinkNode next

//如果当前节点不为空(这里意思是 如果传进来的list 有下一个节点就继续执行)        

        while(null!=cru){

//取出当前节点的下一个节点        

            next = cru.getNext()

//把前一个节点赋予当前节点的下一个节点(这里产生实际改变)

            cru.setNext(pre)

//把当前节点变量赋予前一个节点的变量            

            pre=cru

//把下一个节点变量赋予当前

            cru = next            

        }

//循环体内会循环到方法传入的LinkNode 没有前一个节点为止

//因为几次交换的原因

//因为循环结束,所以把next赋空

        list.setNext(null)

//因为循环的原因,前一个节点实际是第一个节点        

        list=pre

//返回第一个节点        

        return list

    }

    public static void main(String[] args) {

        LinkNode head = new LinkNode(0)   

        LinkNode tmp = null   

        LinkNode cur = null   

         for (int i = 1 i < 10 i++) {   

             tmp = new LinkNode(i)   

             if (1 == i) {   

                 head.setNext(tmp)   

             } else {   

                 cur.setNext(tmp)   

             }   

             cur = tmp   

         }  

         LinkNode h = head   

         while (null != h) {   

             System.out.print(h.getVal() + " ")   

             h = h.getNext() 

         }

             head = reversal(head)   

             System.out.println("\n**************************")   

             //打印反转后的结果   

             while (null != head) {   

                 System.out.print(head.getVal() + " ")   

                 head = head.getNext()   

         }   

         }

    }

我觉得应该是效率问题,如何不做反转在重新计算hash值后将要获得当前链表的最后一个元素,然后对最后一个元素的next属性添加一个节点信息,但是如果反转的话就不用了。

例子:

void transfer(Entry[] newTable, boolean rehash) {

        int newCapacity = newTable.length

        for (Entry<K,V> e : table) {

            while(null != e) {

                Entry<K,V> next = e.next

                //重新计算hash值

                if (rehash) {

                    e.hash = null == e.key ? 0 : hash(e.key)

                }

                int i = indexFor(e.hash, newCapacity)

                //这个是反转的写法 start

                e.next = newTable[i]

                newTable[i] = e

                e = next

                // 反转end 

                //这个是不反转的写法 start

                                

                 Entry<K,V> newEntry = newTable[i]

                 e.next = null

                 if(newEntry != null){

 

                     while(true){

                         if(newEntry.next == null){

                             newEntry.next = e

                             break

                         }

                         

                         newEntry = newEntry.next

                     }

                 }else{

                     newTable[i] = e

                 } 

                 

                e = next

                //不反转 end

                

            }

        }

    }

定义一个LinkedList<Integer>templist = new LinkedList<>()来存储list里面的值,通过迭代list,将值插入在templist的头上,那么templist就是list的反转了,最后将templist赋值给list就行了!

如下代码:

public void reverse() {

LinkedList<Integer> list = new LinkedList<>()

LinkedList<Integer> templist = new LinkedList<>()

int i = 0

while (i < 6) {

list.add(i)

i++

}

Iterator<Integer> it = list.iterator()

int m

while (it.hasNext() && i >= 0) {

m = it.next()

templist.addFirst(m)

i--

}

list = templist

System.out.println(list)

}

运行结果为:

5 4 3 2 1 0

从API中可以看到List等Collection的实现并没有同步化,如果在多线程应用程序中出现同时访问,而且出现修改操作的时候都要求外部操作同步化;调用Iterator操作获得的Iterator对象在多线程修改Set的时候也自动失效,并抛出java.util.ConcurrentModificationException。这种实现机制是fail-fast,对外部的修改并不能提供任何保证。

Iterator是工作在一个独立的线程中,并且拥有一个 mutex锁,就是说Iterator在工作的时候,是不允许被迭代的对象被改变的。

Iterator被创建的时候,建立了一个内存索引表(单链表),这个索引表指向原来的对象,当原来的对象数量改变的时候,这个索引表的内容没有同步改变,所以当索引指针往下移动的时候,便找不到要迭代的对象,于是产生错误。

List、Set等是动态的,可变对象数量的数据结构,但是Iterator则是单向不可变,只能顺序读取,不能逆序操作的数据结构,当 Iterator指向的原始数据发生变化时,Iterator自己就迷失了方向。

所以如果像下面这么写就会抛出异常java.util.ConcurrentModificationException

public void reverse() {

LinkedList<Integer> list = new LinkedList<>()

int i = 0

while (i < 6) {

list.add(i)

i++

}

Iterator<Integer> it = list.iterator()

int m

while (it.hasNext() && i >= 0) {

m = it.next()

list.add(m)

list.remove(0)

i--

}

System.out.println(list)

}