1. 程式人生 > >反轉連結串列 有頭節點和無頭節點

反轉連結串列 有頭節點和無頭節點

有頭節點

參考:https://blog.csdn.net/blioo/article/details/62050967

linkList reverse(linkList head){
  linkList p,q,pr;
  p = head->next;
  q = NULL;
  head->next = NULL;
  while(p){
    pr = p->next;
    p->next = q;
    q = p;
    p = pr;
  }
  head->next = q;
  return head;
}

無頭節點

 

public ListNode ReverseList(ListNode head) {
    	
    	ListNode p,q,pr;
    	p=head.next;
    	q=null;
    	head.next=null;
    	while(p!=null)
    	{
    	pr=p.next;
    	p.next=q;
    	q=p;
    	p=pr;
    		
    	}
    	head.next=p;
		return head;
    	
             
    }