面试题24:翻转链表

19 篇文章 1 订阅

题目:定义一个函数,输入一个链表的头节点,翻转该链表并输出反转后链表的头节点。

解题思路:我们在调整节点的i的m_pNext指针时,除了需要知道节点i本身,还需要知道i的前一个节点h,因为我们需要把i的m_pNext指向节点H。同时我们需要保存i的下一个节点j,以防止链表断开。因此,我们需要定义3个指针,分别指向当前遍历的节点,它的前一个节点,它的后一个节点。

#include<iostream>
#include<stack>
using namespace std;
struct ListNode
{
	int m_nkey;
	ListNode* m_pNext;	
};

 //建立一个链表用头插法
 void AddToHead(ListNode** pHead,int value)
{
 	 
 	ListNode* pNew = new ListNode();
 	pNew->m_nkey = value;
 	pNew->m_pNext = NULL;
 	if(*pHead == NULL)
 	{
 		*pHead = pNew;
	}
	else
	{
//	 	ListNode *pNode = *pHead;
//	 	pNew->m_pNext = pNode;
//	 	pNode->m_pNext = pNew;
//	 	*pHead = pNode;
		pNew->m_pNext = *pHead;
		*pHead = pNew;
		
	}
} //删除列表中的节点
 //删除链表中重复的节点
 
  //翻转链表
  ListNode* Reverse(ListNode* pHead)
  {
  	ListNode* reverseHead = NULL; 
  	ListNode* preNode = NULL;
  	ListNode* pNode = NULL;
  	ListNode* pNext = NULL;
  	pNode = pHead;
  	while(pNode!=NULL)
  	{
  		pNext = pNode->m_pNext;
  		if(pNext==NULL)
  		{
  			reverseHead = pNode;
		  }
  		pNode->m_pNext = preNode;
  		preNode = pNode;
  		pNode = pNext;
  		
	  }
	return reverseHead;  
   } 
  void print(ListNode * pHead)
  {
  	ListNode* pNode = pHead;
	  while(pNode!=NULL)
	  {
	  	cout<<pNode->m_nkey<<" ";
	  	pNode = pNode->m_pNext;
	   } 
   } 
int main()
{
	int num = 10;
	
	ListNode *p  = new ListNode();
	p = NULL;
	ListNode **pHead = &p;
       AddToHead(pHead,1);
	   AddToHead(pHead,2);	
	   AddToHead(pHead,3);	
	   AddToHead(pHead,4);	
	   AddToHead(pHead,5);	
	   AddToHead(pHead,6);	
	   AddToHead(pHead,7);		   		   	   	
	   //DeleteDuplication(pHead);
	   print(*pHead);
	  ListNode* res = Reverse(*pHead);
	  
	  // ListNode *res =  Find(*pHead, 1);
	  // cout<<res->m_nkey;
//	ListNode *del = new ListNode();
	print(res);
 } 
 
 

JAVA实现:

package jianzhi_offer;

public class DeleteDuplication {

	public static class ListNode{
		private int data;
		private ListNode next;
		public ListNode(int data,ListNode next)
		{
			this.data = data;
			this.next = next;
		}
	}
	
	
	//翻转链表
	public static ListNode Reverse(ListNode head)
	{
		ListNode reverseHead = null;
		ListNode preNode = null;
		ListNode pNode = null;
		ListNode pNext = null;
		pNode = head;
		while(pNode!=null)
		{
			pNext = pNode.next;
			if(pNext == null)
				reverseHead = pNode;
			pNode.next = preNode;
			preNode = pNode;
			pNode = pNext;
		}
		return reverseHead;		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode tail = new ListNode(7,null);
		ListNode c = new ListNode(6,tail);
		ListNode b = new ListNode(5,c);
		ListNode a = new ListNode(4,b);
		ListNode e = new ListNode(3,a);
		ListNode d = new ListNode(2,e);
		ListNode head = new ListNode(1,d);
		//deleteDuplication(head);
//	  ListNode res = Find(head,2);
//	  if(res!=null)
//	  System.out.println(res.data+"  ");
		ListNode reverseHead = Reverse(head);
		
		while(reverseHead!=null)
		{
			System.out.print(reverseHead.data + " ");
			reverseHead = reverseHead.next;
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值