Topic:
Flip Linked list
Flip a linked list
Sample Example
Given a list of 1->2->3->null, this flipped list is 3->2->1->null
challenges
One-time rollover complete in situ
Solving:
A recursive return can be solved.
Java Program:
/*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; * } * } */ Public classSolution {/** * @paramhead:the head of linked list. * @return: The new head of reversed linked list. */ PublicListNode Reverse (listnode head) {//Write your code here if(Head = =NULL|| Head.next = =NULL) returnHead; ListNode Second=Head.next; Head.next=NULL; ListNode Res=reverse (second); Second.next=Head; returnRes; }}
View Code
Total time: 2079 Ms
Python program:
"""Definition of Listnodeclass ListNode (object): Def __init__ (Self, Val, next=none): Self.val = Val Self . Next = Next"""classSolution:"""@param head:the First node of the linked list. @return: You should return the head of the reversed linked list. Reverse it in-place. """ defreverse (self, head):#Write your code here ifHead = = NoneorHead.next = =None:returnHead Second=Head.next; Head.next=None Res=Self.reverse (second) Second.next=HeadreturnRes
View Code
Python submission and the same as the title has been pending, should be the background maintenance of what and so on .... The above program is not tested.
Lintcode Easy title: Reverse Linked list Flip linked list