39 39 votes The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure. The function is called with the list containing the integers $1, 2, 3, 4, 5, 6, 7$ in the given order. What will be the contents of the list after the function completes execution? struct node {int value; struct node *next;); void rearrange (struct node *list) { struct node *p, *q; int temp; if (!list || !list -> next) return; p = list; q = list -> next; while (q) { temp = p -> value; p -> value = q -> value; q -> value = temp; p = q -> next; q = p ? p -> next : 0; } } $1, 2, 3, 4, 5, 6, 7$ $2, 1, 4, 3, 6, 5, 7$ $1, 3, 2, 5, 4, 7, 6$ $2, 3, 4, 5, 6, 7, 1$ Data Structures gateit-2005 data-structures linked-list normal + – Ishrat Jahan 13.8k views answer comment Share Follow Print See all 7 Comments 7 7 Comments reply Show 4 previous comments Chhotu commented Oct 22, 2017 reply Follow flag Redundant --> https://gateoverflow.in/485/gate2008-62 10 10 replyShare Arjun commented Oct 22, 2017 reply Follow flag Good catch -- rare to see an exact copy in previous questions :) 12 12 replyShare राजकुमारी विसर्पी commented Oct 31, 2023 i reshown by राजकुमारी विसर्पी Nov 1, 2023 reply Follow flag $ GATE CSE 2008 | Question: 62 $ 0 0 replyShare Please log in or register to add a comment.
Best answer 37 37 votes It is (B) $2, 1, 4, 3, 6, 5, 7$: As, $p$ and $q$ are swapping each other where $q$ is $p \rightarrow next $ all the time. sumit kumar answered Nov 14, 2014 • edited Jun 14, 2018 by Milicevic3306 sumit kumar comment Share Follow See 1 comment 1 1 comment reply Arjun commented Nov 14, 2014 reply Follow flag Yes. Thats what it is doing :) 4 4 replyShare Please log in or register to add a comment.
11 11 votes struct node {int value; struct node *next;); void rearrange (struct node *list) { struct node *p, *q; int temp; if (!list || !list -> next) return; // just check 1st time two adjacent node are there or not. if not program is finish . for example take one node or 0 node in linked list output will be same as input. p = list; q = list -> next; two pointer to point two adjacent node p point first node q point second node which is adjacent to first. while (q) this will check always two adjacent present or not like if at one point only p point some node and q point to null then loop stop running. { 1.temp = p -> value; 2.p -> value = q -> value; 3.q -> value = temp; till here swapping done via temp variable. 4.p = q -> next; just point next node 5.q = p ? p -> next : 0; this will one if p point to some node then q point to its next node. if p point to null then q also point 0. } } Finally while loop take care two node present to swap . if one node present at any time between running of program then while loop terminate. Prashant. answered Nov 24, 2015 Prashant. comment Share Follow See all 2 Comments 2 2 Comments reply Gurdeep Saini commented Jan 3, 2019 reply Follow flag for this q = p ? p -> next : 0; which one is correct (1) or (2) ?? 0 0 replyShare Harshada commented Jan 6, 2019 reply Follow flag 1st is correct. 2 2 replyShare Please log in or register to add a comment.