24、逆リンクリスト(reverseList)
1. パイソン
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur ,pre = head,None
while cur:
tmp= cur.next
cur.next=pre
pre =cur
cur=tmp
return pre
2. Java
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur= head,pre=null;
while(cur!=null){
ListNode tmp=cur.next;
cur.next=pre;
pre=cur;
cur=tmp;
}
return pre;
}
}
3. C ++
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur =head;ListNode * pre = NULL;
while(cur!=NULL){
ListNode * tmp = cur->next;
cur->next = pre;
pre=cur;
cur=tmp;
}
return pre;
}
};
4. C
struct ListNode* reverseList(struct ListNode* head){
struct ListNode * cur=head;struct ListNode * pre=NULL;
while(cur!=NULL){
struct ListNode * tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
return pre;
}
5. JavaScript
var reverseList = function(head) {
var cur=head;var pre=null;
while(cur){
var tmp = cur.next;
cur.next=pre;
pre=cur;
cur=tmp
}
return pre;
};