# Python实现链表倒序（带头指针）

```class node:
def __init__(self, value):
self.value = value
self.next = None

def reverse(head):
if head is None or head.next is None or head.next.next is None:
return head

if head.next.next.next is None:
t = head.next.next
t.next = head.next
head.next.next = None
head.next = t
return head

pre = head.next
cur = pre.next
pre.next = None
next = cur.next
while next.next is not None:
cur.next = pre
pre = cur
cur = next
next = next.next
head.next = next
next.next = cur
cur.next = pre
return head

def out(head):
if head is not None:
while head.next is not None:
print(head.value)
head = head.next
print(head.value)
else:
print(None)

if __name__ == "__main__":
head = node(None)
pre = head
for i in range(6):
pre.next = node(i)
pre = pre.next
out(head)
head = reverse(head)
out(head)```

## 20140428 宏定义 单链表翻转 野指针 1.宏定义swap,加括号有什么意义 #define swap(x,y) x = (x)+(y):y=(x)-(y):x=(x)-(y) 加括号是为了处理表达式参数(即宏的参数可能是个算法表达式)时不出错,因为宏替换就是文本替换,所以如果有以下情况: #define COM(A,B) (A)*(B) 那么COM(6+5,3),它会换成这样: (6+5)*(3) 但是如是去掉了定义中括号,即写成这样: #define COM(A,B) A*B 那么COM(6+5,3),它就会换成这样:6+5*3

## 给定单向链表的头指针和一个结点指针，定义一个函数在O(1)时间删除该结点

#include <iostream> #include <string.h> #include <stdlib.h> #include <stack> using namespace std; struct Node { int data; struct Node* next; }; struct Node* create_list(int len) { if (len <= 0) return NULL; struct Node* head; st

## python数据结构链表之单向链表 ## reorder-list——链表、快慢指针、逆转链表、链表合并

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 由于链表尾端不干净,导致fast->next!=NULL&&fast->next-&

## 对带头结点的单链表的简单操作

#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<memory.h> #define DataType int           //int 可以改写为其它数据类型 typedef struct Node { DataType data; struct Node *next; }Node,*pNode;          //定义结点结构体