Pseudo code: Iterative solution
Ex : Input List: 1->2->3->4->5->6
Head Point to : 1st Node
NextPtr Points to : 2nd Node
1) Have two pointers. (tempptr and nextptr ).
2) Initialize nextptr to next of head ptr ( nextptr points to 2 w.r.t example) and assign next of head ptr to NULL.
3) If nextptr is not NULL in that case copy next node address of nextptr into tempptr.(tempptr = nextptr->next).
4) Assign next node of nextptr as head node.(nextptr->next = head).
5) Assign nextptr as new head node.(head = nextptr).
6) Copy tempptr value in nextptr
7) Start from step 3 and if condition failed in that case new linked list form with head ptr is reverse one.
Note :
Linked list with head ptr grows as : 1 to 2>1 to 3->2-1 to 4->3->2->1 to 5->4>3>2>1 to 6->5->4->3->2->1.
Ex : Input List: 1->2->3->4->5->6
Head Point to : 1st Node
NextPtr Points to : 2nd Node
1) Have two pointers. (tempptr and nextptr ).
2) Initialize nextptr to next of head ptr ( nextptr points to 2 w.r.t example) and assign next of head ptr to NULL.
3) If nextptr is not NULL in that case copy next node address of nextptr into tempptr.(tempptr = nextptr->next).
4) Assign next node of nextptr as head node.(nextptr->next = head).
5) Assign nextptr as new head node.(head = nextptr).
6) Copy tempptr value in nextptr
7) Start from step 3 and if condition failed in that case new linked list form with head ptr is reverse one.
Note :
Linked list with head ptr grows as : 1 to 2>1 to 3->2-1 to 4->3->2->1 to 5->4>3>2>1 to 6->5->4->3->2->1.
No comments:
Post a Comment