[小算法] 找出单链表中的中间元素

两种情况:

1. 链表有奇数个结点, 中间元素只有一个;

2. 链表有偶数个结点, 中间元素会有两个;

 

#include  " stdafx.h "
#include <iostream>
using  namespace std;

struct Link
{
     int Data;
    Link *Next;
};

void PrintMiddleNode(Link *head);

int _tmain( int argc, _TCHAR* argv[])
{
     struct Link head, node1,node2,node3,node4,node5;
    head.Data = - 1;
    head.Next = &node1;
    node1.Data =  1;
    node1.Next = &node2;
    node2.Data =  2;
    node2.Next = &node3;
    node3.Data =  3;
    node3.Next = &node4;
    node4.Data =  4;
    node4.Next = &node5;
    node5.Data =  5;
    node5.Next = NULL;

    PrintMiddleNode(&head);
    cout << endl;

    cin. get();
     return  0;
}

void PrintMiddleNode(Link *head)
{
     if(head == NULL || head->Next == NULL)  return;
    Link *singleStepPointer = head;
    Link*doubleStepPointer = head;

     while( true)
    {
         if(doubleStepPointer->Next != NULL)
        {
            doubleStepPointer = doubleStepPointer->Next;
             if(doubleStepPointer->Next != NULL)
            {
                doubleStepPointer = doubleStepPointer->Next;
            }
             else // 奇数个
            {
                 // middle node should be singleStepPointer->Next
                cout <<  " Middle node is:  " << singleStepPointer->Next->Data;
                 break;
            }
        }
         else // 偶数个
        {
             // middle nodes should be current singleStepPointer and singleStepPointer->Next
            cout <<  " Middle nodes are:  " << singleStepPointer->Data <<  "  and  " << singleStepPointer->Next->Data;
             break;
        }

        singleStepPointer = singleStepPointer->Next;
    }
}

 

转载于:https://www.cnblogs.com/KevinPan/archive/2012/04/18/2455724.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 定义一个结构体来表示单链表的节点,包含一个数据域和一个指向下一个节点的指针: ``` struct ListNode { int val; struct ListNode* next; }; ``` 2. 实现删除相同元素算法,具体步骤如下: (1)定义一个指针p指向头节点,一个指针q指向p的下一个节点; (2)循环遍历单链表,直到p指向尾节点为止; (3)如果p节点的值和q节点的值相同,就将q节点从链表删除,即令p的next指向q的next,释放q节点的内存空间; (4)如果p节点的值和q节点的值不同,就将指针p和q都向后移动一个节点; (5)重复执行步骤(3)和(4),直到p指向尾节点为止。 ``` void deleteDuplicates(struct ListNode* head) { struct ListNode* p = head; while (p != NULL && p->next != NULL) { struct ListNode* q = p->next; if (p->val == q->val) { p->next = q->next; free(q); } else { p = q; } } } ``` 3. 完整代码如下: ``` #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode* next; }; void deleteDuplicates(struct ListNode* head) { struct ListNode* p = head; while (p != NULL && p->next != NULL) { struct ListNode* q = p->next; if (p->val == q->val) { p->next = q->next; free(q); } else { p = q; } } } int main() { struct ListNode* node1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* node2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* node3 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* node4 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* node5 = (struct ListNode*)malloc(sizeof(struct ListNode)); node1->val = 1; node2->val = 2; node3->val = 2; node4->val = 3; node5->val = 3; node1->next = node2; node2->next = node3; node3->next = node4; node4->next = node5; node5->next = NULL; printf("Before deleting duplicates: "); struct ListNode* p = node1; while (p != NULL) { printf("%d ", p->val); p = p->next; } printf("\n"); deleteDuplicates(node1); printf("After deleting duplicates: "); p = node1; while (p != NULL) { printf("%d ", p->val); p = p->next; } printf("\n"); free(node1); free(node2); free(node3); free(node4); free(node5); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值