Reverse a singly linked list

Reverse a singly linked list.

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverseList(ListNode* head) {
12         if (head == NULL){
13             return head;
14         }
15         ListNode *p1,*p2,*p3;
16         p1 = head;
17         p2 = p1->next;
18         if (p2 == NULL){
19             return head;
20         }
21         p3 = p2->next;
22         while (p2 != NULL){
23             p2->next = p1;
24             p1 = p2;
25             p2 = p3;
26             if (p3 != NULL){
27                 p3 = p3->next;
28             }
29         }
30         head->next = NULL;
31         return p1;
32
33     }
34 };
时间: 2024-10-08 20:11:56

Reverse a singly linked list的相关文章

LeetCode 206 Reverse a singly linked list.

Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 递归的办法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v

Reversed Linked List(Reverse a singly linked list)

struct ListNode { int m_nKey; ListNode* next; } ListNode* reverseList(ListNode* pHead) { ListNode* pReversedHead = nullptr; ListNode* pNode = pHead; ListNode* pPrev = nullptr; while(pNode != nullptr){ ListNode* pNext = pNode->next; if(pNext == nullpt

单链表反转(Singly Linked Lists in Java)

单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 Java代码   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } Java代码   package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[])

[cc150] check palindrome of a singly linked list

Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse and compare. 另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式: 0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0 CC150里面给出的Code,非常简洁,贴在下面: length == 1 对应

Singly Linked List

Singly linked list storage structure:typedef struct Node{ ElemType data; struct Node *next;}Node; typedef struct Node *LinkList; LinkedList without head node: LinkedList with head node: Operations: /*check the size of link list.*/int ListLength(LinkL

To find the kth to Last Element of a Singly Linked List

To find the kth to Last Element of a Singly Linked List To find the kth to Last Element of a Singly Linked List Web Link Description Code - C Tips Web Link None Description Write a pro-gram to find the kth to Last Ele-ment of a Singly Linked List For

Reverse a Singly LinkedList

Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ 这是面试的时候被问到的一题,还是考察LinkedList的理解,最后如何得到reverse之后的head的. public Class Soluti

待字闺中之快排(QuickSort)单向链表(Singly Linked List)

题目来源,待字闺中,原创@陈利人 ,欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的快速排序一样,都需要找到一个pivot元素.或者节点.然后将数组或者单向链表划分为两个部分,然后递归分别快排. 针对数组进行快排的时候,交换交换不同位置的数值,在分而治之完成之后,数据就是排序好的.那么单向链表是什么样的情况呢?除了交换节点值之外,是否有其他更好的方法呢?可以修改指针,不进行数值交换.这可以获取更高的效率. 在修改指针的过程中,会产生新的头指针以及尾指针,要记录下来.在par

Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 基本思路: 中序遍历的过程,与有序链表的顺序是一一对应的. 采用中序遍历构造进树的构造. 并利用取值范围,确定,根的位置,以及子树的范围. 故需要遍历整个链表,求得总的长度. 链表长度的一半,即为根结点所在位置. 左边则为左子树的取值范围,右边即为右子树的取值范围. 同上,递归应