Reverse Singly Linked List | Data Structures using C

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

ความคิดเห็น • 123

  • @NareshIT
    @NareshIT  5 ปีที่แล้ว +8

    Please Comment, Subscribe and Click Bell🔔🔔🔔 Icon for More Updates. To learn software course from our experts please register here for online training: goo.gl/HIB0wL

    • @gauravgogoinitsilchar4146
      @gauravgogoinitsilchar4146 4 ปีที่แล้ว

      sir how is the arrow link is working , link is not pointing to anything in the program!!!

  • @sandeepgouda543
    @sandeepgouda543 6 ปีที่แล้ว +26

    sir your video are literally worth enough ..!!apart from this your knowledge your gesture your way of explanations set the platform to different level..!! hands up to u sir👏👏

  • @LiveLifeee
    @LiveLifeee 5 ปีที่แล้ว +14

    You are an excellent teacher sir , you clear the concepts so well . So glad i found this channel.

  • @neethus9848
    @neethus9848 5 ปีที่แล้ว +5

    That was excellent .. I went through so many videos to get the concept and honestly this was the best...

  • @kaustavpaul199
    @kaustavpaul199 7 ปีที่แล้ว +11

    u r great sir...make tough pblm very easy

  • @Justdoing185
    @Justdoing185 3 ปีที่แล้ว

    I am not tired of to comment on your every vedio ,👍👍👍

  • @soumikdutta77
    @soumikdutta77 6 ปีที่แล้ว +1

    Sir your video is worth watching .. Never had such clear conception ... a thank you wont be enough !

  • @nafisnawalnahiyan5032
    @nafisnawalnahiyan5032 5 ปีที่แล้ว +1

    Love and respect form Bangladesh ! You are an awesome teacher Sir !

  • @86kuntalmohan
    @86kuntalmohan 7 ปีที่แล้ว +2

    Sir, your explanation is great! I am very thanks full to you.

  • @goyaldeekshant
    @goyaldeekshant 4 ปีที่แล้ว +13

    HERE IS THE COMPLETE CODE: It includes all functions of linked list tought in this channel. To watch the code of only this video, Please see the void reverse() function only:
    #include
    #include
    struct node
    {
    int data;
    struct node* link;
    };
    struct node* root = NULL;
    int len;
    void append();
    void addatbegin();
    void addatafter();
    int length(void);
    void display();
    void delete();
    void reverse();
    void main()
    { int ch;
    while(1)
    {
    printf("Single linked list operations are:
    ");
    printf("1.Append
    ");
    printf("2.AddAtBegin
    ");
    printf("3.AddAtAfter
    ");
    printf("4.length
    ");
    printf("5.delete
    ");
    printf("6.display
    ");
    printf("7.Reverse
    ");
    printf("8.Exit
    ");
    printf("enter your choice: ");
    scanf("%d",&ch);
    printf("Your choice was: %d
    ",ch);
    switch(ch)
    {
    case 1: append();
    break;
    case 2: addatbegin();
    break;
    case 3: addatafter();
    break;
    case 4: len = length();
    printf("length is %d
    ",len);
    break;
    case 5: delete();
    break;
    case 6: display(root);
    break;
    case 7: reverse();
    break;
    case 8: exit(0);
    default : printf("invalid input
    ");
    }
    }
    }
    void append()
    {
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
    printf("Enter node data
    ");
    scanf("%d",&temp->data);
    temp->link=NULL;
    if(root==NULL)
    {
    root= temp;
    }
    else
    {
    struct node* p;
    p=root;
    while(p->link!=NULL)
    {
    p=p->link;
    }
    p->link=temp;
    }
    }
    void display(struct node* temp)
    {
    if(temp==NULL)
    {
    printf("list is empty
    ");
    }
    else
    {
    printf("elements are: ");
    while (temp != NULL)
    {
    printf(" %d ", temp->data);
    temp = temp->link;
    }
    }
    printf("
    ");
    }
    int length()
    {
    struct node* temp;
    int count=0;
    temp=root;
    while(temp!=NULL)
    {
    count++;
    temp=temp->link;
    }
    return count;
    }
    void addatbegin()
    {
    printf("to be build
    ");
    }
    void addatafter()
    {
    struct node* temp;
    int loc;
    printf("Enter location after which, node to be inserted:
    ");
    scanf("%d",&loc);
    if(loc>length())
    {
    printf("invalid input position:
    ");
    printf("current list is having %d nodes only",length());
    }
    else
    {
    int i=1;
    struct node* p=root;
    while(ilink;
    i++;
    }
    temp=(struct node*)malloc(sizeof(struct node)); //new node creation
    printf("enter new node data:
    ");
    scanf("%d",&temp->data);
    temp->link=NULL;
    //now just link this new node
    //first make right connection
    temp->link=p->link; //right connection
    p->link=temp; // left connection
    }
    }
    void delete()
    {
    struct node* temp;
    int loc;
    printf("Enter node number to be delete: ");
    scanf("%d",&loc);
    if(loc>length())
    printf("invalid position input
    ");
    else if (loc==1)
    {
    temp=root;
    root=temp->link;
    temp->link=NULL;
    free(temp);
    }
    else
    { int i=1;
    struct node* p=root;
    struct node* q=p;
    while(ilink;
    i++;
    }
    q=p->link;
    p->link=q->link;
    q->link=NULL;
    free(q);
    }
    }
    void reverse()
    {
    struct node* p=root;
    struct node* q=root;
    int i=0,temp, j=length()-1;
    while(idata;
    p->data=q->data;
    q->data=temp;
    p-p->link;
    q=root;
    i++;
    j--;
    }
    }

    • @mahadifamgate2686
      @mahadifamgate2686 3 ปีที่แล้ว

      very thank you. can u give me book name (or website) of this code. for learn more.

    • @sumitrana9999
      @sumitrana9999 2 ปีที่แล้ว

      its not optimised.(w.r.t interview)

  • @Sylie1111
    @Sylie1111 5 ปีที่แล้ว

    I watched ur complete playlist...that's really helpful for fresher students

  • @crackcodinginterview4995
    @crackcodinginterview4995 4 ปีที่แล้ว

    Simple and to the point Naresh!

  • @gamingultimate7489
    @gamingultimate7489 3 ปีที่แล้ว

    very,very,,,,,,,nice sir your videos your explanation morvelessss

  • @yarramneninikhil1299
    @yarramneninikhil1299 5 ปีที่แล้ว

    Sir, u have given us a wonderful explanation

  • @anjalikaranam6134
    @anjalikaranam6134 7 ปีที่แล้ว +14

    Sir can u explain merging of two linked lists please sir

  • @finetechie5550
    @finetechie5550 7 ปีที่แล้ว +1

    Sir, Please upload All sorting programs
    Sir, I am a great fan of yours teaching. I studied C from you in Ameerpet and studying data structure from youtube...
    Please...Uploading all sorting programs.ASAP as it great time for us in Bangalore to get a job.

  • @nurullahsany2379
    @nurullahsany2379 4 ปีที่แล้ว

    Easy to understand ..Thank you

  • @villansfftelugugaming5345
    @villansfftelugugaming5345 3 ปีที่แล้ว

    Sir nijamga mi antha clear ga evaru cheypaleru miku Nijam ga johar entha cheypina takuvey sir mi gurunchi

  • @dipankar9700
    @dipankar9700 4 ปีที่แล้ว

    Sir you are awesome..😄😄

  • @shouvikdutta2825
    @shouvikdutta2825 5 ปีที่แล้ว

    fabulous explanation.

  • @riadhallouch
    @riadhallouch 3 ปีที่แล้ว

    thank u so much this is life saviiing man

  • @sjamal4403
    @sjamal4403 7 ปีที่แล้ว

    Sir your lectures are best

  • @adarshkv4616
    @adarshkv4616 5 ปีที่แล้ว

    Thank you sir very great explainanation

  • @PriyankaSingh-dk1zi
    @PriyankaSingh-dk1zi 5 ปีที่แล้ว +1

    very helpful for me

  • @indrapratapyadav1166
    @indrapratapyadav1166 5 ปีที่แล้ว

    More helpful video, sir.tq

  • @swathithatikonda6722
    @swathithatikonda6722 4 ปีที่แล้ว

    U r amazing sir!!!! :)

  • @naveenearth2243
    @naveenearth2243 6 ปีที่แล้ว +2

    Sir please upload the video about addition of two polynomial

  • @yashwanthl9295
    @yashwanthl9295 4 ปีที่แล้ว

    God of Data Structure

  • @002_abhinavsingh3
    @002_abhinavsingh3 4 ปีที่แล้ว

    Thank You

  • @karthikareddy1720
    @karthikareddy1720 6 ปีที่แล้ว

    tq sir ,clear expalnation

  • @harshitgoyal2382
    @harshitgoyal2382 5 ปีที่แล้ว

    Pass kra diya bhaishab aapne

  • @shashwatdev2371
    @shashwatdev2371 4 ปีที่แล้ว

    Thanks👌

  • @kprakash9665
    @kprakash9665 7 ปีที่แล้ว +1

    good xplanation

  • @sjamal4403
    @sjamal4403 7 ปีที่แล้ว

    Best explanation

  • @vineeth8612
    @vineeth8612 2 ปีที่แล้ว

    Instead of reinitialising q we can add j-i in the while loop. I guess it computes to the same

  • @shariqueshaikhh
    @shariqueshaikhh 6 ปีที่แล้ว

    Thank you so much sir!

  • @poojithamadakasira7295
    @poojithamadakasira7295 4 ปีที่แล้ว

    Sir will you please explain reversing singly linked list elements along with there corresponding addresses.

  • @chetansahu1505
    @chetansahu1505 5 ปีที่แล้ว +5

    This magic happens in the time complexity at the order of N square.

    • @ansalvi8943
      @ansalvi8943 4 ปีที่แล้ว

      Yes time complexity is expirational time rather than using swapping method he should have been using 3 pointers.....

  • @debajyotidey1999
    @debajyotidey1999 5 ปีที่แล้ว +1

    Sir , in case of implementing the reverse linked list , why length is used?

  • @romymarry7604
    @romymarry7604 4 ปีที่แล้ว

    Thank u sir

  • @ArunKumar-vg7xi
    @ArunKumar-vg7xi 6 ปีที่แล้ว

    Good explanation sir

  • @kurrucharan9376
    @kurrucharan9376 5 ปีที่แล้ว

    Thank you sir..

  • @lovingmyself1196
    @lovingmyself1196 3 ปีที่แล้ว

    Tqqqqq. Sir

  • @sowbarnikab2124
    @sowbarnikab2124 2 ปีที่แล้ว

    sir what about the time complexity of the code given?

  • @karthikareddy1720
    @karthikareddy1720 6 ปีที่แล้ว +2

    sir plzz implement all logics in pc or laptop sir

  • @suresha6276
    @suresha6276 8 ปีที่แล้ว

    very nice explanation could you please post reverse double linked list and tress like BST,AVL tress operations,hashing also iam looking for your posts please post ASAP

  • @soubhikdas3997
    @soubhikdas3997 6 ปีที่แล้ว

    sir,u are initially choosing the elements in increasing order.so in case of reversing swapping helps.what if the elements are not sorted

    • @soubhikdas3997
      @soubhikdas3997 6 ปีที่แล้ว

      @Aditya Darade no i was saying that to avoid problem sir intentionally choosed sorted elements and that helped in reversing.

    • @soubhikdas3997
      @soubhikdas3997 6 ปีที่แล้ว

      @Aditya Darade what if initially the chosen elements are not sorted.?

    • @susheelyadav6143
      @susheelyadav6143 6 ปีที่แล้ว

      reversing or swapping is not done on the basis of node data,its based on there position(address). Here we did not interfere with node data.

  • @entrtnmnthub5357
    @entrtnmnthub5357 5 ปีที่แล้ว

    Awesome

  • @prabhpreetkaur7758
    @prabhpreetkaur7758 7 ปีที่แล้ว

    Sir ,can you show how will you create struct Node in which you can store the root and pointers and also best and worst cases!
    Thanks!

  • @faisalalmamun4312
    @faisalalmamun4312 7 ปีที่แล้ว

    just awesome

  • @shobitshobit
    @shobitshobit 7 ปีที่แล้ว +1

    i wish you were using java :( but i liked your videos :) please make videos in data structure in java.

  • @pushparay5554
    @pushparay5554 6 ปีที่แล้ว

    thank u

  • @radartelevision7634
    @radartelevision7634 3 ปีที่แล้ว +1

    awesome video sir but you didnt define the variable k

  • @rewasone8826
    @rewasone8826 7 ปีที่แล้ว

    sir what we do when eleemnt in array is 5
    if u swap 0with4 and 1wit3 and then which elemet weselect to swap wit loc 2 element

    • @ManyuDhyani
      @ManyuDhyani 7 ปีที่แล้ว +1

      at time of loc 2, I=j=2 and as in while loop (I

  • @sauravhateme
    @sauravhateme 4 ปีที่แล้ว

    One question, I thought root will point to last node in the linked list and address will be reversed.

    • @codingguru4865
      @codingguru4865 4 ปีที่แล้ว

      Hi Saurav,
      Check this
      Full Playlist of Data Structure
      Explained very easily
      th-cam.com/video/gh9siMkdLkM/w-d-xo.html

  • @keshavburud7584
    @keshavburud7584 4 ปีที่แล้ว

    Sir, can't we take i=1 and j=len??

  • @sjamal4403
    @sjamal4403 7 ปีที่แล้ว

    Please explain other topics of data structures

  • @shubhshree5419
    @shubhshree5419 6 ปีที่แล้ว

    good Sir

  • @akhilalli5332
    @akhilalli5332 6 ปีที่แล้ว

    Super

  • @aatifnazar8203
    @aatifnazar8203 5 ปีที่แล้ว +3

    Isnt this algorithm O(N2)?

  • @venkateshdammala7
    @venkateshdammala7 4 ปีที่แล้ว

    Sir, I executed the code but reverse order of linked list is not coming perfectly..it is reversing in inorder sir, please help me in this anybody

  • @rajanagendra7042
    @rajanagendra7042 5 ปีที่แล้ว

    sir, i need to learn data structures completely but i did not find data structures in online training

  • @kissantv7431
    @kissantv7431 4 ปีที่แล้ว

    Respected Sir
    I have understood very well but having problem in printing the reverse linked list how to print it can you please explain in text or any other way I'll be very thankful to you

  • @RohitSingh-gg5qw
    @RohitSingh-gg5qw 6 ปีที่แล้ว

    make the videos on data structure using java, please

  • @ts-ch6zo
    @ts-ch6zo 5 ปีที่แล้ว

    👏👏

  • @amankrsingh
    @amankrsingh 5 ปีที่แล้ว +1

    Sir please explain how i become 0 and J become 3

  • @karthikareddy1720
    @karthikareddy1720 6 ปีที่แล้ว

    plzz implement all operations in laipi sir

  • @ChSureshLeBKIET
    @ChSureshLeBKIET 2 ปีที่แล้ว

    What happens if the array consists 7 elements

  • @sushmitaroy9776
    @sushmitaroy9776 7 ปีที่แล้ว

    Sir, don't we need to reverse the link part too?

    • @harishkumarramsagarvanganu4257
      @harishkumarramsagarvanganu4257 5 ปีที่แล้ว +1

      No need. We should not care about the link (Adrees of next node which we can't predict). It's only the matter is the data (elements)

  • @holyshit922
    @holyshit922 7 ปีที่แล้ว

    Reverse function can be written simpler by changing only links

  • @sampakundu5483
    @sampakundu5483 5 ปีที่แล้ว

    K variable is not declared. And temp pointer variable also.

  • @percussionistbypassion2931
    @percussionistbypassion2931 6 ปีที่แล้ว +3

    and we need to create a local variable temp by using "struct node* temp"

    • @venkateshdammala7
      @venkateshdammala7 4 ปีที่แล้ว +1

      We need to declare temp as int type ..not struct node* because it is Storing int type data

  • @behindthescene4406
    @behindthescene4406 6 ปีที่แล้ว

    Sir why there is nested while , I can't get that can any one please explain me

    • @anirudhsilverking5761
      @anirudhsilverking5761 6 ปีที่แล้ว

      The first while loop is used for increasing i and decreasing j until i

  • @kavithapriyacj4868
    @kavithapriyacj4868 4 ปีที่แล้ว +1

    Sir u mistakenly explained as I=0 and j=4 after first iteration actually it's I=1 and j=3

  • @RahulKumar-pt2su
    @RahulKumar-pt2su 5 ปีที่แล้ว

    🙏🙏🙏🙏

  • @hometvfirestick
    @hometvfirestick 3 ปีที่แล้ว

    complete this playlist and arrange videos in correct manner

  • @adarshkv4616
    @adarshkv4616 5 ปีที่แล้ว

    Sir why you are writing after i++ and j--
    P P.data
    Q root
    Pls explain sir😔😔

    • @shreyashchauhan8567
      @shreyashchauhan8567 5 ปีที่แล้ว +1

      learn pointers, Structures and then watch all the videos from this playlist, you will know. In short it is a structure member

    • @prabha20028
      @prabha20028 4 ปีที่แล้ว

      We r taking the p pointer to pointer to next node..and q pointer to point to the previous node (u can see q node in inner while loop)..i mean in order to swap all elements of linkes list we have to make the p pointer to point to next node and q pointer point to previous node...
      Hope u got clarify man.
      Happy coding😉

  • @AniKet-bw7vv
    @AniKet-bw7vv 4 ปีที่แล้ว

    for this method, it is mandatory that the list should be arranged in ascending order first otherwise it fails

    • @rayeesdot310
      @rayeesdot310 4 ปีที่แล้ว +1

      It is all done by loop's .
      Actually we Traverse lists via loop

  • @supriyagangadhar2896
    @supriyagangadhar2896 6 ปีที่แล้ว

    how 'i' value is 0 and 'j' value is 3 at 14:12 min

    • @supriyagangadhar2896
      @supriyagangadhar2896 6 ปีที่แล้ว +1

      'i' will be incremented and 'j' will be decremented right,? then i=1 and j=3 is correct

    • @manjyotsinghchaudhary6624
      @manjyotsinghchaudhary6624 6 ปีที่แล้ว +1

      yes,u are correct

    • @nareshdosi5754
      @nareshdosi5754 6 ปีที่แล้ว +1

      He should have basically said that k =0 instead of i.

  • @freddyzhou3130
    @freddyzhou3130 5 ปีที่แล้ว +6

    this is a BAD implementation to reverse single linked list! Because there is a great overhead when swapping the data of 2 nodes, and the time complexity is O(n^2) because you have to access linked list each time when going to the position j ! Besides, you can Google for a best solution, maybe it's about O(n) time and just swap the pointer but not the data!

  • @ameyamahadevgonal8130
    @ameyamahadevgonal8130 4 ปีที่แล้ว

    Hats off

  • @venkatgambhir9913
    @venkatgambhir9913 4 ปีที่แล้ว

    Why k is used

  • @RishabMenonR
    @RishabMenonR 7 ปีที่แล้ว

    All the program implementations are available here :)
    houseofgeek.in/single-linked-list-operations/

  • @amitprasad8114
    @amitprasad8114 7 ปีที่แล้ว +1

    Explanation is good . But solution is not efficient ,space and time complexity will be more.

  • @nagarajukolar2121
    @nagarajukolar2121 4 ปีที่แล้ว

    Radix sort

  • @rocker4037
    @rocker4037 3 ปีที่แล้ว

    //all sll oprations
    #include
    #include
    using namespace std;
    struct node{
    int data;
    node *next;
    };
    struct node *temp,*temp1,*head,*tem;
    int len=0;
    //head=NULL;
    void insertatend(){
    temp=new node;
    int e;
    coute;
    temp->data=e;
    temp->next=NULL;
    if(head==NULL){
    head=temp;
    }
    else{
    temp1=head;
    while(temp1->next!=NULL){
    temp1=temp1->next;
    }
    temp1->next=temp;
    }
    }
    void display(){
    if(head==NULL){
    coutnext;
    temp->next=tem;
    }
    }
    void delet(){
    int pos;
    coutpos;
    if(head==NULL){
    coutnext=tem->next;
    free(tem);}}
    int length(){
    temp=head;
    while(temp!=NULL){
    len++;
    temp=temp->next;
    }
    return len;
    }
    void reverse(){
    int i=0;
    int j=length();
    node *p,*q;
    //p=new node;
    //q=new node;
    p=head;
    q=head;
    int t,k;
    while(idata;
    p->data=q->data;
    q->data=t;
    i++;
    j--;
    q=head;
    p=p->next;
    }
    cout

  • @govindprajapati5524
    @govindprajapati5524 6 ปีที่แล้ว

    sir all things are good but sir you didn't reversed middle element

    • @nehakumar6831
      @nehakumar6831 6 ปีที่แล้ว

      Middle elements stay at middle only

  • @indianlegendking9071
    @indianlegendking9071 7 หลายเดือนก่อน

    You are saying arrays

  • @nagarajukolar2121
    @nagarajukolar2121 4 ปีที่แล้ว

    Heap sort

  • @asmdferdousulhaque8425
    @asmdferdousulhaque8425 5 ปีที่แล้ว +1

    Not effective in terms of complexity, sorry can't agree

  • @shinratenseiratbuvid6938
    @shinratenseiratbuvid6938 5 ปีที่แล้ว

    it was so hard

    • @prabha20028
      @prabha20028 4 ปีที่แล้ว

      I dont think so..🙄🙄

  • @rkprojects6143
    @rkprojects6143 2 ปีที่แล้ว

    Not efficient n optimized algo

  • @pradyumnanayak8603
    @pradyumnanayak8603 5 ปีที่แล้ว +1

    not effective program . time complexity will be n^2 for it .

    • @lavishgarg8616
      @lavishgarg8616 5 ปีที่แล้ว

      can you can give an alternative to it if yes then do

  • @anusha1676
    @anusha1676 5 ปีที่แล้ว

    Super