首页 > 其他 > 详细

leetcode——链表

时间:2019-10-22 00:09:26      阅读:65      评论:0      收藏:0      [点我收藏+]
  1. 206. 反转链表(易)

    反转一个单链表。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL
    c++:
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            ListNode *newHead=NULL;
            while(head){
                ListNode *next=head->next;
                head->next=newHead;
                newHead=head;
                head=next;
            }
            return newHead;
        }
    };
    

     python:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseList(self, head: ListNode) -> ListNode:
            newHead=None
            while head!=None:
                nxt=head.next
                head.next=newHead
                newHead=head
                head=nxt
            return newHead
    

     java:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode newHead=null;
            while(head!=null){
                ListNode next=head.next;
                head.next=newHead;
                newHead=head;
                head=next;
            }
            return newHead;
        }
    }
    
  2.  



     

 

leetcode——链表

原文:https://www.cnblogs.com/mcq1999/p/11717051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!