๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day 35 ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day 35

habbn 2024. 2. 12. 04:05
728x90

๐Ÿ“†2024.2.11

 

์˜ค๋Š˜ ๊ณผ์ œ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋‹ค ํ’€์—ˆ๋‹ค!! ์‚ฌ์‹ค ์Šค์Šค๋กœ ํ˜ผ์ž ํ’€์—ˆ๋‹ค๊ณ ๋Š” ํ•  ์ˆ˜ ์—†๊ณ  ์ฑ— GPT์˜ ๋„์›€๊ณผ ๋™๋ฃŒ๋ถ„๋“ค์˜ ๋„์›€์œผ๋กœ ํ’€์—ˆ๋Š”๋ฐ ๊ทธ๋ž˜๋„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ดํ•ดํ•˜๊ณ  ๋„˜๊ธด ๋ถ€๋ถ„์€ ๋ฟŒ๋“ฏํ•˜๋‹ค.

๋‚ด์ผ์€ ์Šคํƒ ํ ํ’€๊ณ  CSAPP ์ฑ… ์ข€ ์ฝ์–ด๋ด์•ผ๊ฒ ๋‹ค.

 


 

๊ณผ์ œ - ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

alternateMergeLL
  1. ๋‘ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋ฅผ ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋Š” ์œ„์น˜์— ์‚ฝ์ž…ํ•œ๋‹ค.
  2. ๋‘ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์— ๋ฒˆ๊ฐˆ์•„๊ฐ€๋Š” ์œ„์น˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์‚ฝ์ž…๋˜์–ด์•ผ ํ•œ๋‹ค.

 

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ

void alternateMergeLinkedList(LinkedList *ll1, LinkedList *ll2)
{
    ListNode *L1 = ll1->head;
    ListNode *L2 = ll2->head;
    
    while(L1 != NULL && L2 != NULL)
    {
    	ll2->head = L2->next;  //ll2์˜ head๋ฅผ L2์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
        L2->next = L1->next;   //L2์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ L1์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •
        L1->next = L2;         //L1์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ L2๋กœ ์„ค์ •
        
        L1 = L1->next->next;   //L1์˜ ๋…ธ๋“œ๋ฅผ L1์˜ ๋‹ค์Œ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •
        L2 = ll2->head;	       //L2์˜ ๋…ธ๋“œ๋ฅผ ll2์˜ ํ—ค๋“œ๋กœ ์„ค์ •
        
        ++(ll1->size);
        --(ll2->size);
    }
}

 

chat gpt์˜ ๋„์›€์„ ๋ฐ›์•„ ํ’€์—ˆ๋Š”๋ฐ ์‚ฌ์‹ค ์ข€ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€์„œ ํ’€์ด๋ฅผ ๋ดค๋”๋‹ˆ ํ’€์ด์˜ ๊ตฌํ˜„์ด ๋” ์ดํ•ด๊ฐ€ ๊ฐ€๊ณ  ๊ฐ„๋‹จํ•œ ๊ฑฐ ๊ฐ™๋‹ค.

 

 

ํ’€์ด
void alternateMergeLinkedList(LinkedList *ll1, LinkedList *ll2)
{
    ListNode *temp;
    int fixsize1 = ll1->size;
    int fixsize2 = ll2->size;
    int inspos = 1; 
    
    if(fixsize1 >= fixsize2)
    {
    	while(ll2->size > 0)
        {
            temp = findNode(ll2, 0);
            insertNode(ll1, inspos, temp->item);
            removeNode(ll2,0);
            inspos +=2;
         }
     }
     else{
     	while(fixsize1 > 0)
        {
            temp = findNode(ll2,0);
            insertNode(ll1, inspos, temp->item);
            removeNode(ll2,0);
            inspos +=2;
            fixsize1 -= 1;
         }
     }
 }

 

ll1์˜ size๊ฐ€ ll2์˜ size๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด findNode()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ll2์˜ ๊ฐ’์„ temp์— ๋„ฃ๊ณ  ll1์˜ 1,3,5... ๋“ฑ ์‚ฌ์ด์‚ฌ์ด์— insert ํ•˜๊ณ  ll2์˜ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•ด ์ฃผ๊ณ ,

 

ll1์˜ size๊ฐ€ ll2์˜ size๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋™์ผํ•˜๊ฒŒ ์‚ฌ์ด์‚ฌ์ด์— ๋„ฃ์–ด์ฃผ๋ฉด์„œ fixsize๋ฅผ 1์”ฉ ์ค„์—ฌ์ฃผ๋ฉด์„œ fixsize์˜ ๊ธธ์ด๋งŒํผ๋งŒ ์‚ฝ์ž…ํ•ด ์ฃผ๋„๋ก ํ•œ๋‹ค.

์ด ๊ตฌํ˜„ ๋ฐฉ์‹์ด ๋” ์ดํ•ด๊ฐ€ ๊ฐ€๊ณ  ๊น”๋”ํ•œ ๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.

 

 

moveOddItemsToBackLL
  1. ํ™€์ˆ˜๋ฅผ ๋ชจ๋‘ ๋’ค๋กœ ์˜ฎ๊ธด๋‹ค.

์ด ๋ถ€๋ถ„์„ ํ’€๋ฉด์„œ ๋งŽ์€ ์‹œ๊ฐ„์„ ํ• ์• ํ•ด์„œ, ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ๋™๋ฃŒ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด ๋ดค๋Š”๋ฐ odd๋ฅผ ์ฐพ๊ณ  ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  while๋ฌธ ์•ˆ์—์„œ ๊ฐ™์ด ๋Œ๋ ค์„œ(?) ์•ˆ ๋๋˜ ๊ฑฐ ๊ฐ™๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ดํ•ดํ•˜๋Š” ๋ถ€๋ถ„์—์„œ๋„ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ์ง€๋งŒ, ์ฝ”๋“œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋””๋ฒ„๊น…ํ•ด์„œ ํ™•์ธํ•ด ๋ณด๋‹ˆ ์œ ๋ ˆ์นด.. ๋ฅผ ์™ธ์น˜๊ฒŒ ๋˜์—ˆ๋‹ค..

 

void moveOddItemsToBack(LinkedList *ll)
{
	/* add your code here */
	if (ll == NULL || ll->head == NULL )	
		return;
	
	ListNode *cur = ll->head;
	ListNode *prev, *odd, *tail = NULL;
	
	while (cur != NULL)
	{
		//ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ํ™€์ˆ˜์ด๋ฉด
		if(cur->item % 2 != 0)
		{	
			if(odd == NULL)		//odd ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
			{
				odd = cur;		//ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ odd ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋กœ ์„ค์ •
				tail = cur;		//ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ tail๋กœ ์„ค์ •
			}
			else
			{
				tail->next = cur;	//tail์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์„ค์ •
				tail = cur;			//tail์„ ํ˜„์žฌ ๋…ธ๋“œ(๋งˆ์ง€๋ง‰ ๋…ธ๋“œ)๋กœ ์„ค์ •
			}
			//์ด์ „๊ฐ’ ์—†์œผ๋ฉด
			if(prev == NULL)
			{
				ll->head = cur->next;
			}
			else
			{
				prev->next = cur->next;	//ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ด์ „ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •
			}
		}
		//์ง์ˆ˜์ด๋ฉด
		else
		{
			prev = cur;		//์ด์ „ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
		}
		cur = cur->next;	//ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
	}

	//odd๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด
	if(odd != NULL)
	{
		tail->next = NULL;		
		
		if(prev == NULL)
		{
			ll->head = odd;		
		}
		else
		{
			prev->next = odd;	
		}
	}
}

 

 

moveEvenItemsToBackLL
  1. ์ง์ˆ˜๋ฅผ ๋ชจ๋‘ ๋’ค๋กœ ์˜ฎ๊ธด๋‹ค.

์ด ๋ฌธ์ œ๋Š” ํ™€์ˆ˜ ๋ฌธ์ œ๋ž‘ ๋˜‘๊ฐ™์•„์„œ ๋ณ€์ˆ˜๋ž‘ ์ง์ˆ˜ ์ฐพ๋Š” ๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

void moveEvenItemsToBack(LinkedList *ll)
{
	/* add your code here */
	if(ll == NULL && ll->head == NULL)
		return;

	ListNode *cur = ll->head;
	ListNode *even , *prev , *tail = NULL;

	while(cur != NULL)
	{
		if(cur->item %2 == 0)
		{
			if(even ==NULL)
			{
				even = cur;
				tail = cur;
			}
			else
			{
				tail->next = cur;
				tail = cur;
			}
			if(prev ==NULL)
			{
				ll->head = prev;
			}
			else
			{
				prev->next = cur->next;
			}
		}
		//ํ™€์ˆ˜์ด๋ฉด
		else
		{
			prev = cur;
		}
		cur = cur->next;
	}
	if(even !=NULL)
	{
		tail->next =NULL;
		if(prev ==NULL)
		{
			ll->head = even;
		}
		else
		{
			prev->next = even;
		}
	}
}

 

 

frontBackSplitLL

 

  1. ๋‹จ์ผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„ํ• ํ•œ๋‹ค.
  2. ํ•˜๋‚˜๋Š” ์ „๋ฐ˜๋ถ€(frontList), ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ํ›„๋ฐ˜๋ถ€(backList)๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  3. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ, ์ถ”๊ฐ€ ์›์†Œ๋Š” ์ „๋ฐ˜๋ถ€ ๋ฆฌ์ŠคํŠธ์— ํฌํ•จํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋™๋ฃŒ๋ถ„๊ป˜์„œ ํ”Œ๋กœ์ด๋“œ์˜ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‘์šฉํ•ด์„œ ํ’€๋ฉด ๋œ๋‹ค๋ผ๊ณ  ํ•˜๋ฉด์„œ ์•Œ๋ ค์ฃผ์…จ๋‹ค.

ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ฒ˜์Œ ๋“ค์–ด๋ดค๋‹ค. ์ด๋Ÿฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋Š”์ง€๋„ ๋ชฐ๋ž๋‹ค.

์‹œ๊ฐ„์ด ๋œ๋‹ค๋ฉด ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ๊ณต๋ถ€ํ•ด ๋ด์•ผ๊ฒ ๋‹ค.

 

void frontBackSplitLinkedList(LinkedList *ll, LinkedList *resultFrontList, LinkedList *resultBackList)
{
    ListNode *temp1 = ll->head;  
    ListNode *temp2 = ll->head->next;
   
    while(temp2 != NULL)           //๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€
    {
    	temp2 = temp2->next;       //temp2๋ฅผ ํ•œ ์นธ ์•ž์œผ๋กœ ์ด๋™(์ด ๋‘ ์นธ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ)
        if(temp2 != NULL)          //์ด๋™ํ•œ temp2๊ฐ€ null์ด ์•„๋‹ˆ๋ผ๋ฉด
        {
            temp1 = temp1->next;	//temp1 ํ•œ ์นธ์”ฉ ๋” ์ด๋™
            temp2 = temp2->next;	//temp2 ํ•œ ์นธ์”ฉ ๋” ์ด๋™ (์ด ๋‘ ์นธ ์ด๋™)
        }
    }
    resultFrontList->head = ll->head;  
    resultFrontList->size = ll->size / 2; 
    resultBackList->head = temp1->next;   //temp1์€ ์ค‘๊ฐ„์— ์œ„์น˜
    resultBackList->size = ll->size - resultFrontList->size;
    
    temp1->next = NULL;     //๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์™„์ „ํžˆ ๋ถ„๋ฆฌ
}

 

temp1์€ ํ•œ ๋…ธ๋“œ์”ฉ ์ด๋™ํ•˜๊ณ , temp2๋Š” ๋‘ ๋…ธ๋“œ์”ฉ ์ด๋™ํ•œ๋‹ค. 

temp2๊ฐ€ NULL์ด ๋œ๋‹ค๋Š” ๋ง์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋๊นŒ์ง€ ๋‹ค ์ด๋™ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ temp2๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด temp1์€ ์ค‘๊ฐ„์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.

 

resultFrontList->head๋ฅผ ll->head๋กœ ์„ค์ •ํ•ด ์ฃผ๊ณ , size๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ์ˆ˜๋กœ ์„ค์ •ํ•œ๋‹ค.

 

resultBackList->head๋ฅผ temp1->next๋กœ ์„ค์ •ํ•ด ์ฃผ๋Š” ์ด์œ ๋Š”

temp1 ๋ฆฌ์ŠคํŠธ๋Š” ์ค‘๊ฐ„์— ์œ„์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— temp1์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ head๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

resultBackList->size๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด ์‚ฌ์ด์ฆˆ์—์„œ resultFrontList์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋บ€ ๊ฐ’์ด ๋œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  temp1->next๋ฅผ NULL๋กœ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ๋กœ ์™„์ „ํžˆ ๋ถ„๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, temp1์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ null๋กœ ํ•ด์คŒ์œผ๋กœ์จ ์™„์ „ํžˆ ๋ถ„๋ฆฌ๋œ๋‹ค.

-> ์‹ค์ œ ์ด ์ฝ”๋“œ๋ฅผ ์•ˆ ์ผ๋‹ค๊ฐ€ ์—๋Ÿฌ๊ฐ€ ๋œจ๊ณ , ๋‘ ๋ฆฌ์ŠคํŠธ๋กœ ์™„์ „ํžˆ ๋ถ„๋ฆฌ๊ฐ€ ์•ˆ์—ˆ๋‹ค. 

-> resultFrontList : 1 2 3 4 5 6

-> resultBackList : 5 6

 

 

moveMaxToFront
  1. ์ตœ๋Œ€ํ•œ ํ•œ ๋ฒˆ๋งŒ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  2. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•œ ๋…ธ๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ์•ž์ชฝ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋น„๊ต์  ๊ธˆ๋ฐฉ ํ’€์—ˆ๋‹ค.

int moveMaxToFront(ListNode **ptrHead)
{
    ListNode *temp = *ptrHead;    //๋ฆฌ์ŠคํŠธ์˜ head๋ฅผ ๊ฐ€๋ฆฌํ‚ด
    ListNode *prev = NULL;	    
    ListNode *max = *ptrHead;    
    
    if(temp ==NULL)	   //head๊ฐ€ null์ด๋ฉด
    	return 0;
    
    while(temp->next != NULL)    //๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€
    {
    	if(temp->next->item > max->item)
        {
            prev = temp;        //์ด์ „ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์„ค์ •
            max = temp->next;   //max๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
        }
        temp = temp->next;      //๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
     }
     if(max != *ptrHead)        //๋งŒ์•ฝ max๊ฐ€ ํ—ค๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด
     {
        prev->next = max->next;  //prev์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ max์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ 
        max->next = *ptrHead;    //max์˜ next๋ฅผ ํ—ค๋“œ๋กœ
        *ptrHead = max;          //ํ—ค๋“œ๋ฅผ max๋กœ ์—…๋ฐ์ดํŠธ
     }
     return 0;
}

 

์ „์ฒด์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)

 

recursiveReverse
  1. ์ฃผ์–ด์ง„ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ next ํฌ์ธํ„ฐ์™€ head ํฌ์ธํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์žฌ๊ท€์ ์œผ๋กœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

-> ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘์–ด์•ผ ํ•œ๋‹ค.

 

๋งˆ์ง€๋ง‰์— ํ˜„์šฐ๋‹˜์ด ์„ค๋ช…ํ•ด ์ฃผ์…”์„œ ์„ค๋ช… ๋“ฃ๊ณ  pdf ํŒŒ์ผ์— ์žˆ๋Š” ํžŒํŠธ ์ฃผ์›Œ ๋‹ด์•„์„œ ๊ตฌํ˜„ํ•˜๋‹ˆ๊นŒ ๋๋‹ค.

void RecursiveReverse(ListNode **ptrHead)
{
     if(*ptrHead == NULL || (*ptrHead)->next == NULL)  //๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๊ฑฐ๋‚˜, ๋ฆฌ์ŠคํŠธ์— ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋ผ๋ฉด
     	return;
     
     ListNode *first = *ptrHead;
     ListNode *rest = first->next;
     
     RecursiveReverse(&rest);
     
     //ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ์˜ ๋‹ค์Œ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ
     first->next->next = first;
     first->next = NULL;    //๋’ค์ง‘ํžŒ ๋ฆฌ์ŠคํŠธ์˜ ๋์„ ํ‘œ์‹œํ•œ๋‹ค.
     
     *ptrHead = rest;       //์ƒˆ๋กœ์šด ํ—ค๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
}

 

๋ฆฌ์ŠคํŠธ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)

 

728x90