zoukankan      html  css  js  c++  java
  • 单链表的反转

    单链表的反转,每次循环需要四步骤。

      public ListNode reverse(ListNode head)
    	{
    		if(head == null || head.next == null)
    		{
    			return head;
    		}
    		ListNode pPre = head;
    		ListNode pCurr = head.next;
    		ListNode pNext = null;
    		head.next = null;
    		while(pCurr != null)
    		{
    			pNext = pCurr.next;
    			pCurr.next = pPre;
    			pPre = pCurr;
    			pCurr = pNext;
    		}
    		return pPre;
    	}
    
    public static ListNode reverse(ListNode head)
    {
    	if(head == null || head.next == null)
    	{
    		return head;
    	}
    	ListNode pPre = new ListNode(0);
    	pPre.next = head;
    	ListNode pCurr = head, pNext = null;
    	while(pCurr != null)
    	{
    		pNext = pCurr.next;
    		pCurr.next = pPre;
    		pPre = pCurr;
    		pCurr = pNext;
    	}
    	head.next = null;//head最后再赋值为null
    	return pPre;
    }
    

      

  • 相关阅读:
    租房子查询练习
    投票练习题
    多条件查询
    查询
    练习---新闻界面
    mysql增删改处理
    挖宝游戏
    mysql数据访问
    练习···表格
    类的使用
  • 原文地址:https://www.cnblogs.com/masterlibin/p/5630131.html
Copyright © 2011-2022 走看看