Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
链表反转问题。
/**************************************************************************
*
* 206. [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
*
* Input: 1->2->3->4->5->NULL
* Output: 5->4->3->2->1->NULL
**************************************************************************/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode NODE;
NODE *reverseList(NODE *head){
if (NULL == head || NULL == head->next) return head;
NODE *pre = NULL;
NODE *cur = head;
NODE *next= cur->next;
while (cur) {
cur->next = pre;
pre = cur;
cur = next;
if (next)
next = next->next;
}
return pre;
}
NODE *reverseList(NODE *head){
if (NULL == head || NULL == head->next) return head;
NODE *p = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return p;
}
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
/**************************************************************************
*
* 7. [Reverse Integer](https://leetcode.com/problems/reverse-integer/)
*
* Given a 32-bit signed integer, reverse digits of an integer.
* if outside the signed 32-bit integer range [-231, 231 - 1], then return 0
*
* Example:
* 123 => 321
* -123 => -321
* 120 => 21
* -2147483648 => 0
**************************************************************************/
int reverse(int x){
long long num = x;
long long res = 0;
for ( ; num; num/=10)
res = res*10 + num % 10;
if (res > INT_MAX || res < INT_MIN)
return 0;
else
return res;
}